aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Primitive variable declaration doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Primitive variable declaration doubts" Watch "Primitive variable declaration doubts" New topic
Author

Primitive variable declaration doubts

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

I've confusion, when i compile them there is no error so what is sense of these two versions of data types ,now java is sucking me
Can anyone tell differences between them ?


Where are the rules ? ,it means i can use anything like that int or Int, char or Char ,Byte or byte, Double or double etc???


Tell the difficulties that i am difficult.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1773
    
    7

The first creates a primitive short with value 7.
The second creates a Short object, using auto-boxing to wrap the literal short value.
Before Java 5 the second appraoch would fail, and you would have to use the Short constructor that takes a primitive short.
As of Java 5 the second auto-boxing approach is equivalent to Short.valueOf(7) (this overload of valueOf was also added in Java 5).


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

The difference is 'short' is a primitive data type whereas 'Short' is a class - which wraps primitive 'short'.

I'm not sure, but the only reason to (now) maintain primitive data types is backward compatibility and (perhaps) low memory footprint.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Jelle Klap wrote:The first creates a primitive short with value 7.
The second creates a Short object, using auto-boxing to wrap the literal short value.
Before Java 5 the second appraoch would fail, and you would have to use the Short constructor that takes a primitive short.
As of Java 5 the second auto-boxing approach is equivalent to Short.valueOf(7) (this overload of valueOf was also added in Java 5).


Thanks Jelle but i m not getting this

Where are the rules ? ,it means i can use anything like that int or Int, char or Char ,Byte or byte, Double or double etc???
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Jelle Klap wrote:As of Java 5 the second auto-boxing approach is equivalent to Short.valueOf(7)

Is it so? I thought it is
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:The difference is 'short' is a primitive data type whereas 'Short' is a class - which wraps primitive 'short'.

I'm not sure, but the only reason to (now) maintain primitive data types is backward compatibility and (perhaps) low memory footprint.

I hope this helps.


Thanks Anayonkar if Short is class then what is short ? method or something else ?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

You are welcome.

saloni jhanwar wrote:if Short is class then what is short ? method or something else ?

No. Its just a primitive data type. A primitive data type is predefined by language and is treated as a reserved keyword.

On another note, wrapper class for int is Integer (not Int).

I hope this helps.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:You are welcome.

saloni jhanwar wrote:if Short is class then what is short ? method or something else ?

No. Its just a primitive data type. A primitive data type is predefined by language and is treated as a reserved keyword.

On another note, wrapper class for int is Integer (not Int).

I hope this helps.


Thanks, please tell me wrapper classes for other primitives also like for boolean,char,byte,double,float and long.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1773
    
    7

Anayonkar Shivalkar wrote:
Jelle Klap wrote:As of Java 5 the second auto-boxing approach is equivalent to Short.valueOf(7)

Is it so? I thought it is


No boxing uses the valueOf() / xxxValue() pairs.
You can check this yourself using javap to dissasamble a .class file.
For instance:


Yields the following byte code using javap:
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Wrapper classes names are same as primitive data types - just first letter is capital (e.g. Boolean, Float).

Exceptions : Integer for int and Character for char.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:Wrapper classes names are same as primitive data types - just first letter is capital (e.g. Boolean, Float).

Exceptions : Integer for int and Character for char.


Thanks Anayonkar
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Wrapper class is a wrapper around a primitive data type. It represents primitive data types in their corresponding class instances e.g. a boolean data type can be represented as a Boolean class instance. All of the primitive wrapper classes in Java are immutable i.e. once assigned a value to a wrapper class instance cannot be changed further.

Following table lists the primitive types and the corresponding wrapper classes:

Primitive Wrapper
---------------------------------
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Jelle Klap wrote:boxing uses the valueOf() / xxxValue() pairs.
You can check this yourself using javap to dissasamble a .class file.

Thank you (I should've checked it btw)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primitive variable declaration doubts