• Post Reply Bookmark Topic Watch Topic
  • New Topic

String in java  RSS feed

 
narayan choudhary
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All....
in c++ the declaration of String is like : char str []="Manythings";
Here str is pointer who points to the location of M .
But in java the declaration of string is like : String str ="Manythings";
Here str is an object .
How an object can point the location of M.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narayan choudhary wrote:How an object can point the location of M.

Welcome to the Ranch!

str is not an object. It is a reference to some object of type String.
In Java you do not need to know how the reference points to the actual object. It is handled by JVM for you.
It can be hard to think "Java way" when you have an experience in languages where it matters like C++.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Expunge the words C and C++ from your memory . Java® is a completely different language and does things completely differently from C. C does not support objects at all; Java does. This is obviously a common cause of confusion because it even says so in the Java Language Specification.

In Java a String is an object with all an object's attributes and it stores its data in its own way.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String object does actually store its data as an array of chars, but Java® arrays are different from C arrays and Java® chars are different from C chars.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if what you really want to do is get the char at a particular point in the String, then the class has a method that will return that: charAt(int).
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you really want char array there is toCharArray()
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
narayan choudhary wrote:in c++ the declaration of String is like : char str []="Manythings";
Here str is pointer who points to the location of M .
But in java the declaration of string is like : String str ="Manythings";
Here str is an object .
How an object can point the location of M.

Exactly the same way it does in C++:
Date date = new Date;
the only difference being that in C++ a string is NOT an object (unless you specifically create a String class); it's a pointer to a null-terminated character array.

The other main differences are:
(a) Java references are the ONLY way to "point to" an object; and Java doesn't tell you how they point.
(b) Java references cannot be changed except by assignment.
(c) There are no reference-based expressions in Java like you have in C/C++ - eg: (str + 1), where str is a char*.
(d) (just FYI, although not related to your question) Java arrays are objects.

And believe me, once you get used to it, you'll thank the designers.

HIH

Winston
 
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
narayan choudhary wrote:
in c++ the declaration of String is like : char str []="Manythings";
Here str is pointer who points to the location of M .


To be fair, C++ has string objects for a long time now. So, in C++ (assuming using the std namespace), you create strings like this ...



Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!