• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive variable declaration doubts

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic