Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
Java All : Data Types, Variables and Constants
Concept Notes and Resources
Concept Learning Code Sheets

Variable Declaration and Initialisation #3862

Variable declaration and initialisation varieties are explained here.

class VarDecInit
{
public static void main(String[] args)
{
int x;
x=10;
int y=20;
int z=y;
double d=Math.pow(2,3);
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(d);
}
}

Understanding double and float data type #3840

Double and float data type declaration its max value and sizing.

class DoubleFloat
{
public static void main(String[] args)
{
Double x=2342.34567;
float y=2342.34567f;
System.out.println(x);
System.out.println(y);
Double X=x;
Float Y=y;
System.out.println("Double type max value = "+X.MAX_VALUE);
System.out.println("Float type max value = "+Y.MAX_VALUE);
System.out.println("Double type size = "+X.SIZE);
System.out.println("Float type size = "+Y.SIZE);
}
}

Understanding byte, short, int and long types #3846

Different types of integer data types with its maximum and minimum value limits are explained with this code.

class IntTypes
{
public static void main(String[] args)
{
byte bp=127;
byte bn=-128;
short sp=32767;
short sn=-32768;
int ip=2147483647;
int in=-2147483648;
long lp=9223372036854775807L;
long ln=-9223372036854775808L;
System.out.println("Largest byte type value = "+bp);
System.out.println("Smallest byte type value = "+bn);
System.out.println("Largest short type value = "+sp);;
System.out.println("Smallest short type value = "+sn);;
System.out.println("Largest int type value = "+ip);;
System.out.println("Smallest int type value = "+in);;
System.out.println("Largest long type value = "+lp);
System.out.println("Smallest long type value = "+ln);;
}
}

Understanding char data type #3848

The java char data type and its possible values are explained here.


class CharType
{
public static void main(String[] args)
{
char a=0;
char b=10;
char c=33;
char d=65;
char e=126;
char f=255;
char g=65535;
char h='a';
char i='u03C0';
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
System.out.println(i);
}
}

Understanding boolean data type #3850

The java boolean data type declaration and its features.

class BooleanType
{
public static void main(String[] args)
{
boolean b=true;
//int x=b;
if(b) System.out.println(b);
if(b==true) System.out.println(b);
}
}

Understanding Reference data type #3859

Reference data type for class object reference and array reference.

class RefDataType
{
int rd=5;
public static void main(String[] args)
{
RefDataType robj = new RefDataType();
System.out.println(robj.rd);
int ar[]={1,2};
System.out.println(ar);
System.out.println(ar[0]+", "+ar[1]);
}
}

Variable Kinds – Local, Class and Instance Variables #3865

Mainly used kinds Local, Class and Instance Variables are discussed here.

class VarKinds
{
static int x=1;
int y=2;
public static void main(String[] args)
{
int z=3;
VarKinds ob=new VarKinds();
System.out.println(x);
System.out.println(ob.y);
System.out.println(z);
}
}

Variable Default Values #3867

Java class variables have default values. Let us experiment with that.

class VarDefaultValues
{
static byte b;
static short s;
static int i;
static long l;
static char c;
static double d;
static float f;
static boolean bool;
public static void main(String[] args)
{
System.out.println("Default value of byte "+b);
System.out.println("Default value of short "+s);
System.out.println("Default value of int "+i);
System.out.println("Default value of long "+l);
System.out.println("Default value of char "+c);
System.out.println("Default value of double "+d);
System.out.println("Default value of float "+f);
System.out.println("Default value of boolean "+bool);
}
}

Resolving Class and Local Variable Name Clash #3871

Methods to rightly use variable names when their names clash between class/static types and local types.


class VariableNameClash
{
static int x=10;
public static void main(String[] args)
{
int x=20;
System.out.println(x);
//System.out.println(this.x);
System.out.println(VariableNameClash.x);
}
}

Variable Scope #575

This is a program that will tell you about the scope of variables in different parts of programs.

class VariableScope
{
static int n;
public static void main(String[] args)
{
n=15:
System.out.println("The square of the number is :"+(n*n));
}
}

Getting size of Primitive data types #565

Outputs the size of the eight primitive data types present in java.

class SizeOfPrimitives
{
public static void main (String[] args)
{
System.out.println("Size of char: " + (Character.SIZE) + " bit");
System.out.println("Size of byte: " + (Byte.SIZE) + " bit");
System.out.println("Size of short: " + (Short.SIZE) + " bit");
System.out.println("Size of int: " + (Integer.SIZE) + " bit ");
System.out.println("Size of long: " + (Long.SIZE) + " bit");
System.out.println("Size of float: " + (Float.SIZE) + " bit");
System.out.println("Size of double: " + (Double.SIZE) + " bit");
}
}

Sum and Difference of two numbers #569

Gives you understanding of declaration along with dynamic initialization

class SumDifference
{
public static void main(String[] args)
{
int num1=20;
int num2=40;
int sum=num1+num2;
int dif=num2-num1;
System.out.println("The sum of two numbers is "+sum);
System.out.println("The difference of two numbers "+dif);
}
}

Product and Division of two numbers #571

Product and Division to show data type compatibility difference.

class ProductDivision
{
public static void main (String[] args)
{
int num1=20;
int num2=5;
int num3=num1*num2;
System.out.println("The product is : "+num3);
double num4=num1/num2; //This should be commented when variation below is tried.
//double num4=num1*1.0/num2; //this variation should be tried when
//expected output is not an whole number
System.out.println("The division is : "+num4);
}
}

Average of two double numbers #573

Finding average of two double numbers.

class Average
{
public static void main (String[] args)
{
double num1=23.5;
double num2=12.5;
double avg=(num1+num2)/2;
System.out.println("Average of "+
num1+" and "+
num2+" is : "+avg);
}
}

Printing Ascii Codes as Characters Alphabets & Digits #3512

Printing uppercase alphabets, lowercase alphabets, digits by printing them as characters typecasted from its integer form ascii values.

class CharAndAscii
{
public static void main(String[] args)
{
System.out.println("-- Uppercase characters with their ASCII codes --");
for(int i=65;i<=90;++i) System.out.print(((char)i)+"="+i+" ");
System.out.println("n-- Lowercase characters with their ASCII codes --");
for(int i=97;i<=122;++i) System.out.print(((char)i)+"="+i+" ");
System.out.println("n-- Digits 0 to 9 with their ASCII codes --");
for(int i=48;i<=57;++i) System.out.print(((char)i)+"="+i+" ");
}
}
Solved Problems

Output writing for Data Type Size #3857

Data type size determination

Data type selection #3873

Write a program to initialise a variable with price of an item as a fractiona

Data default values Viva-question #3875

Why do we need to initialise local variables when we have default values spec

Float f suffix Viva-question #3880

Why do we add f suffix to a decimal literal number when we initialise a float

Exam Questions
Quizzes

Concept Notes:1  Code Sheets:15  Solved Problems:5  Exam Questons:9  Quizzes:7
Back