About pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sasimca007
    New Member
    • Sep 2007
    • 129

    #1

    About pointers

    Hello friends,
    Is pointer concept is there in java? If so how to use? if not why?
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by sasimca007
    Hello friends,
    Is pointer concept is there in java? If so how to use? if not why?
    Pointer is not on java.

    The language designers decided to abstract memory management to a higher level in Java than in C. The reason for this is that it is easy to make mistakes using pointers and other lower level memory management techniques. These mistakes can lead to bugs. hard to read code, memory leaks that waste system resources, and security issues.

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      Java actually does have pointers. When you declare an object without initializing it, eg String str;, you're actually getting a pointer. Using new returns a pointer as well to help with stack space when passing large objects to functions. The "pass by reference" method that Java uses to pass objects to functions? It's "pass by pointer" under the hood. Java hides all this pointer functionality to avoid the pitfalls of detailed memory management.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by sukatoa
        Pointer is not on java.
        Java is full of pointers (every object is actually a pointer to an object state (value)).
        Java just doesn't have pointer arithmetic but pointers are all over the place.

        kind regards,

        Jos

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by Laharl
          Java actually does have pointers. When you declare an object without initializing it, eg String str;, you're actually getting a pointer. Using new returns a pointer as well to help with stack space when passing large objects to functions. The "pass by reference" method that Java uses to pass objects to functions? It's "pass by pointer" under the hood. Java hides all this pointer functionality to avoid the pitfalls of detailed memory management.
          Java does not have pointers. The word "pointer" only occurs twice in the JLS, and then only informally. To say Java has pointers would only confuse C programmers and frighten the children.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by BigDaddyLH
            Java does not have pointers. The word "pointer" only occurs twice in the JLS, and then only informally. To say Java has pointers would only confuse C programmers and frighten the children.
            Say no more about that darn NullPointerExce ption then :-)

            kind regards,

            Jos

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by JosAH
              Say no more about that darn NullPointerExce ption then :-)

              kind regards,

              Jos
              This is one of those moments that .NET got it right when it was plagiarizing Java. They renamed it "NullReferenceE xception".

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by BigDaddyLH
                This is one of those moments that .NET got it right when it was plagiarizing Java. They renamed it "NullReferenceE xception".
                I consider that name even more obfuscating because it has nothing to do with
                references as C++'s T& reference to a type T. It's my constitutional fundamental
                right to consider them pointers but no 'rithmetic ;-)

                kind regards,

                Jos

                Comment

                • edwardrsmith
                  New Member
                  • Feb 2008
                  • 62

                  #9
                  Everyone who said that every object in Java is a pointer is correct. A pointer is just an address in memory at which information is stored. Unlike C and C++, pointers are abstracted. That means that it is impossible to explicitly declare a pointer. At the same time it is also easier because there is no need to worry about memory management because of the garbage collector takes care of clearing memory and memory allocation is automated as well.

                  Comment

                  • sukatoa
                    Contributor
                    • Nov 2007
                    • 539

                    #10
                    Originally posted by edwardrsmith
                    Everyone who said that every object in Java is a pointer is correct. A pointer is just an address in memory at which information is stored. Unlike C and C++, pointers are abstracted. That means that it is impossible to explicitly declare a pointer. At the same time it is also easier because there is no need to worry about memory management because of the garbage collector takes care of clearing memory and memory allocation is automated as well.
                    since sasimca007 ask about pointers, i think he/she knows C or C++, he/she was just asking if the pointer he/she have used in C/C++ was also in java...

                    A pointer is just an address in memory at which information is stored. Unlike C and C++
                    You're correct, but no abstract pointer in java that can be used to link such addresses...

                    Correct me if im wrong,
                    Sukatoa

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by sukatoa
                      since sasimca007 ask about pointers, i think he/she knows C or C++, he/she was just asking if the pointer he/she have used in C/C++ was also in java...



                      You're correct, but no abstract pointer in java that can be used to link such addresses...

                      Correct me if im wrong,
                      Sukatoa
                      The best advice you can give a C/C++ programmer moving to Java is to forget ever thing they know about pointers; every hack and fudge and weird pointer arithmetic, and void*, and arrays via pointers and function pointers and ...

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        In other words, whether you are learning Java by your own free will, or are required to learn Java, Welcome; Welcome to Java. It's safer here.

                        </half-life 2 quote>

                        Comment

                        • BigDaddyLH
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1216

                          #13
                          Originally posted by Ganon11
                          In other words, whether you are learning Java by your own free will, or are required to learn Java, Welcome; Welcome to Java. It's safer here.

                          </half-life 2 quote>
                          I recall that when I moved from C++ to Java, I kissed the ground like the old pope.

                          Comment

                          Working...