Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 89
  • Last Modified:

Is String a Primitive datatype or Non-Primitive? (Since String is an Object)

I know String is a Primitive data type, but it can also be an Object.
If it's an Object, how can it be Primitive?


String s1="hello";
String s2=new string ("hello");

Open in new window

0
rgb192
Asked:
rgb192
  • 3
  • 2
  • 2
3 Solutions
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I know String is a Primitive data type

No, String is an Object and always an Object. Why do think otherwise? Is it because of the first line of code above?

If so, that is just the Java Language giving you some special syntax for creating a String Object that is already initialised to a certain value. But it is still an Object.

Same thing as...

Integer i = 3;

i is still an Integer Object, but Java has just done this behind the scenes..

Integer i = new Integer(3);
1
 
it_saigeDeveloperCommented:
No, String is not a primitive data type.  Java has 8 primitive data types; namely - byte, short, int, long, float, double, boolean and char.

Character strings are afforded special support via the java.lang.String class.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

-saige-
0
 
rgb192Author Commented:

i is still an Integer Object, but Java has just done this behind the scenes..
Integer i = new Integer(3);


Is integer an object?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
it_saigeDeveloperCommented:
While int is a primitive, Integer wraps the value of the primitive type int in an object. - Source

All primitives have wrappers.  The wrapper's are used to extend the primitive by adding Conversion, Comparison and Serialization methods (to name a few).

-saige-
1
 
rgb192Author Commented:
could you give an example of a wrapper
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
could you give an example of a wrapper
Wow, an example was in that last post from it_saige... "While int is a primitive, Integer wraps the value of the primitive type int in an object"

The others are...  Long, Short, Double, Float, Character, Boolean and Byte
0
 
rgb192Author Commented:
thanks for int string wrapper
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now