Tutorials for Java ,Core java ,Advance java ,oracle ,HTML, CSS, JavaScript, Java and other programming language tutorial Learn Java tutorial for beginners with examples in eclipse online. Java tutorial point for core java, servlet, jsp, struts, spring, for learning web technologies online. Content includes tutorials and references relating to HTML, CSS, JavaScript, PHP, AngularJS, SQL, Bootstrap, Node.js, and jQuery this is a website like w3school tutorialspoint etc

Search This Blog

About

Header Ads

Friday, 2 March 2018

Understand all points related to java data type in comparison with c language

0 comments

Understand all points related to java data type in comparison with c language 

All integer values represented by 4 data types -byte,short,int,long

By default every integer number is considered as int type,it means when we use any integer no. in a program compiler and jvm will consider this number as int type number .

Class Example{
Void m1(byte b) {
System.out.println("byte-param");
}
void m1(short s) {
system.out.println("short-param");
}
void m1(int i) {
system.out.println("int-param");
}
void m1(long l) {
system.out.println("long-param");
}
Class Test {
public static void main (string[]args) {
Example e1=new example();
e1.m1(5);
byte b=5;
short s=5;
long l=5;

e1.m1(b);
e1.m1(s);
}
}

in above program inside main method we called m1 method by passing the argument 5 compiler and jvm will consider this integer value as int type value then compiler and vm will execute m1(int0 hence we got output int-param .


To represent a integer numbers as byte ,short,long type we must prefix/suffix cast operator or a special alphabet .

Examples-

To represent 50 as byte we must explicitly write below code 

byte b1=50;   -------------variable assignment

(byte)50 --------casting

To represent 50 as short type we must write below code

short s1=50;   -------variabsle assignment

(short)50  ----------casting


To represent integer number 50 as long type we must explicitly write below code  --

long l1=50;   -----------variable assignment

(long)50;    -------cast operator

50l/50L    -------------suffix character


Rule-There is no suffix character to represent integer number as byte and short , we must use as cast operator or, variable assignment as shown in above lines .

In variable assignment we must follow one rule i.e value type and range  must be lesser than variable type range 

for  example  

int i1=5; Right

int i2=5L; Wrong

5 is a int type  ,so allowed to store in int variable .

5L is a long type ,so not allowed to store in int variable .

But we can assign int type value in lessor range datatypes byte and short variables , because these datatypes don't have their own type of special values .
 for example-byte b1=5; right
                      short s1=5; right

Rule- we cant store all integer number in byte and short variables . we can store only the integers which are in the range of byte and short .

byte range is " -128 to 127 "
 short range is "-32,768 to 32,767 "

hence we can store int values in byte and short variables in this range .


byte b2=5000; wrong

short s2=500000; wrong




No comments:

Post a Comment