• Post Reply Bookmark Topic Watch Topic
  • New Topic

pointers in java  RSS feed

 
s john smith
Greenhorn
Posts: 16
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to traverse the array that contains string by incrementing a pointer?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s john smith wrote:is it possible to traverse the array that contains string by incrementing a pointer?


What do you think, and what?

And what do you even mean by "the array that contains string"? I think I know what you're tyring to say, but if I'm right, then you're assuming that Java is more like C than it actually is. What you're saying is this:


but I bet that's not what you mean.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s john smith wrote:is it possible to traverse the array that contains string by incrementing a pointer?


If by "incrementing a pointer" you mean changing the reference, then no. If you mean can you increment the array index to traverse the array, yes.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not have pointers, and therefore no such thing as pointer arithmetic.

 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s john smith wrote:... the array that contains string ...

Do you mean the char array encapsulated in a String instance?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:
s john smith wrote:... the array that contains string ...

Do you mean the char array encapsulated in a String instance?


That's what I'm guessing he means, though it's kind of the opposite of what he said.
 
sanjay kumar gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most studies agree that pointers are one of the primary features that enable programmers to inject bugs into their code.

Given that structures are gone, and arrays and strings are objects, the need for pointers to these constructs goes away. Thus, Java has no pointer data types. Any task that would require arrays, structures, and pointers in C can be more easily and reliably performed by declaring objects and arrays of objects.

Instead of complex pointer manipulation on array pointers, you access arrays by their arithmetic indices. The Java run-time system checks all array indexing to ensure indices are within the bounds of the array.

You no longer have dangling pointers and trashing of memory because of incorrect pointers, because there are no pointers in Java.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Java does not have pointers


JLS 4.3.1 Objects:
The reference values (often just references) are pointers to these objects...


And, um, NullPointerException.

So Java does have pointers, which are the same thing as its references, and very different from C++ references, but somewhat similar to C/C++ pointers, except that we can't see their actual values, manipulate them directly, or do arithmetic on them. Oh, and though they can be thought of as being the address of the object, they're not actually specified to be so.

Right.

I swear, that one sentence from the JLS plus the naming of NPE have combined to lead to more newbie confusion and misconception about how Java works than anything else.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sanjay kumar gupta wrote:Most studies agree that pointers are one of the primary features that enable programmers to inject bugs into their code.

Given that structures are gone, and arrays and strings are objects, the need for pointers to these constructs goes away. Thus, Java has no pointer data types. Any task that would require arrays, structures, and pointers in C can be more easily and reliably performed by declaring objects and arrays of objects.

Instead of complex pointer manipulation on array pointers, you access arrays by their arithmetic indices. The Java run-time system checks all array indexing to ensure indices are within the bounds of the array.

You no longer have dangling pointers and trashing of memory because of incorrect pointers, because there are no pointers in Java.


This sounds like a quote from somewhere. If it is, please give proper credit to the source. If it is in fact your own thoughts in your own words, please accept my apologies and carry on.
 
sanjay kumar gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is from Sun website, I just want to share this knowledge never think about credit ,but yes I will make sure next time
Jeff Verdegan wrote:
sanjay kumar gupta wrote:Most studies agree that pointers are one of the primary features that enable programmers to inject bugs into their code.

Given that structures are gone, and arrays and strings are objects, the need for pointers to these constructs goes away. Thus, Java has no pointer data types. Any task that would require arrays, structures, and pointers in C can be more easily and reliably performed by declaring objects and arrays of objects.

Instead of complex pointer manipulation on array pointers, you access arrays by their arithmetic indices. The Java run-time system checks all array indexing to ensure indices are within the bounds of the array.

You no longer have dangling pointers and trashing of memory because of incorrect pointers, because there are no pointers in Java.


This sounds like a quote from somewhere. If it is, please give proper credit to the source. If it is in fact your own thoughts in your own words, please accept my apologies and carry on.
 
s john smith
Greenhorn
Posts: 16
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am new to java and i was trying to solve the exercise question. this is the question-"Write a function that takes a string as argument and displays it vertically char by char. Traverse the array by incrementing a pointer." after reading this got confused because there is no concept of pointers in java
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s john smith wrote:i am new to java and i was trying to solve the exercise question. this is the question-"Write a function that takes a string as argument and displays it vertically char by char. Traverse the array by incrementing a pointer." after reading this got confused because there is no concept of pointers in java


First of all, the "traverse the array" quote implies either (1) the question means character array instead of string, or (2) you are allowed to get internal array of the string. And with an array, you can access the characters via incrementing the array index.

Henry
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s john smith wrote:this is the question-"Write a function that takes a string as argument and displays it vertically char by char. Traverse the array by incrementing a pointer."


If that's the whole question, then it's a really bad question. The first sentence is sort of fine, except that Java doesn't have functions, it has methods. As for the second sentence... what "array" is that? And (as you say) what's with this "pointer" thing? If it was a question about C programming it might make sense, but as a question about Java programming it's pretty much rubbish.
 
s john smith
Greenhorn
Posts: 16
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if it is char array,how can i use pointer? can you elaborate?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really. If you're taking a course which claims to teach you Java, and the questions you're asking here are representative of what you are learning there, then the money you paid to take that course is being wasted.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s john smith wrote:so if it is char array,how can i use pointer? can you elaborate?

java does not let you use pointer arithmetic the way C/C++ does. You can use a loop and an index to access each element of the char array in order, which may be what they are asking for...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!