Solved

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

Posted on 2016-08-08
8
31 Views
Last Modified: 2016-11-14
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
Comment
Question by:rgb192
  • 3
  • 2
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 167 total points
ID: 41748231
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
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 333 total points
ID: 41748887
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
 

Author Comment

by:rgb192
ID: 41749038

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 333 total points
ID: 41749067
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
 

Author Comment

by:rgb192
ID: 41792986
could you give an example of a wrapper
0
 
LVL 35

Expert Comment

by:mccarl
ID: 41793578
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
 

Author Closing Comment

by:rgb192
ID: 41886854
thanks for int string wrapper
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java. non-English characters encoding problem. intellij idea 3 74
Cipher Configuration on Apache HTTPD 4 53
eclipse shortcuts 9 45
MySqlDump not dumping triggers 1 17
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now