Data types in java

20
Data Types in Java Variables Arrays Practice questions

Transcript of Data types in java

Page 1: Data types in java

Data Types in JavaVariablesArrays

Practice questions

Page 2: Data types in java

Category of Data Types

Page 3: Data types in java

• Integers This group includes byte, short, int, and long, which are for whole-valued signed numbers.

• Floating-point numbers This group includes float and double, which represent numbers with fractional precision.

• Characters This group includes char, which represents symbols in a character set, like letters and numbers.

• Boolean This group includes boolean, which is a special type for representing true/false values.

Page 4: Data types in java

Data Types with default value and memory

Page 5: Data types in java

Program for Data Type and values

public class Demo_datatypes {public static void main(String[] y){

int int_variable=10;float float_variable=(float)10.50;char character_variable=88;char character_variable_char='a';boolean boolean_variable=false;System.out.println("the int value is"+int_variable);System.out.println("the float value is"+float_variable);System.out.println("the char value is"+character_variable);System.out.println("the char value is"+character_variable_char);System.out.println("the boolean value is"+boolean_variable);

} }

Do and see the output

Page 6: Data types in java

Default values for variablespublic class Demo_datatypes {

int int_variable;float float_variable;char character_variable;char character_variable_char;boolean boolean_variable;

public static void main(String[] y){Demo_datatypes d=new Demo_datatypes();System.out.println("the int value is"+d.int_variable);System.out.println("the float value is"+d.float_variable);System.out.println("the char value is"+d.character_variable);System.out.println("the char value is"+d.character_variable_char);System.out.println("the boolean value is"+d.boolean_variable);}

}Do and See the outputGo to Slide-1

Page 7: Data types in java

Variables

• A Java variable is a piece of memory that can contain a data value. A variable thus has a data type.

E.g. country codes, currency codes etc.Sytax :Data Type <VariableName>;

Page 8: Data types in java

Java Variable Naming Conventions

• Java variable names are case sensitive. The variable name money is not the same as Money or MONEY.

• Java variable names must start with a letter, or the $ or _ character.

• After the first character in a Java variable name, the name can also contain numbers

• Variable names cannot be equal to reserved key words in Java. For instance, the words int or for are reserved words in Java.

• See the slide no 5 for example for variable declaration, assigning, reading values from variable

Page 9: Data types in java

Types of Variables in Java

• Static variables• Instance variables• Method local and Method parameters• Block variables

Page 10: Data types in java

Static Variable

• These variables are declared at the top level.• They begins their life when first class loaded

into memory and ends when class is unloaded.

• As they remain in memory till class exist• There only one copy of these variables exist

Page 11: Data types in java

public class Demo_datatypes{

static int a_=10;static void demo()

{System.out.println("the value of a_ is ::"+a_);}public static void main(String[] y){System.out.println("the value of static variable a is"+a_);a_=20;demo();}

}Do and see the output

Page 12: Data types in java

Instance variable

• Variables which are defined without the STATIC keyword and are Outside any method declaration are object specific and are known as Instance variables.

class Page {

public String pageName; // instance variable with public accessprivate int pageNumber; // instance variable with private access

Void demo(){…}}

Page 13: Data types in java

Method local variables / Method parameters

• Method local variables are declared anywhere inside method. their life is started point they declared / initialized and ends when method completes

• Method parameters are local variables to method only except their declaration in parameter list of method and they gets value upon invocation.

Page 14: Data types in java

Example for method local and parameter

public class method_variables {static public void demo(String x){/* * here x is a method parameter * here a is method local variable */int a=10;System.out.println("the value of x is"+x+"\n the value of a is"+a);}public static void main(String[] a){demo("yugandhar");}

}

Do and see the output

Page 15: Data types in java

Block Variables

• These are variables that are declared inside any block. They can be accessed only within that block only.

public class block_variable {public static void main(String[] y){int block_variable=10;System.out.println("the block_variable value is"+block_variable);}

}Do and see the outputGoto Slide-1

Page 16: Data types in java

Arrays

• An array is an indexed collection of fixed number of homogenous data elements

• The main advantage of arrays is we can represent a group of values with single name hence readability of the code will be improved

• The main limitation of arrays is they are fixed in size.i.e once we constructed an array there is no chance of increasing or decreasing bases on our requirement

• The first element of array start with zero.

Page 17: Data types in java

Example for Arrays

public static void main(String[] a){/* * int[] array_variable is Declaring Array * new int[5] is Constructing an Array * array_variable[0]=1 is Initializing Array * array_variable[i] is accessing array */int[] array_variable=new int[5];array_variable[0]=1;array_variable[2]=3;for(int i=0;i<array_variable.length;i++){ System.out.println("the value is"+i+"::"+array_variable[i]);}/* * initilizating array at the time construction only */int[] array_={1,2,3};for(int i=0;i<array_.length;i++){System.out.println("the value array_ is"+i+"::"+array_[i]);}}

Page 18: Data types in java

Multidimensional Arrays

• A multidimensional array is a series of arrays so that each array contains its own sub-array(s).

SyntaxElement type[][] array_name=new element_type[size][size];

Multidimensional arrays rules• When you allocate memory for a multidimensional array,

you need only specify the memory for the first (leftmost) dimension.

• You can allocate the remaining dimensions separately.• In Java the length of each array in a multidimensional array

is under your control.

Page 19: Data types in java

Example ofArrayspublic static void main(String[] a)

{int [][] My_array_variable=new int[2][3];My_array_variable[0][0]=1;My_array_variable[0][1]=2;My_array_variable[0][2]=3;My_array_variable[1][0]=4;My_array_variable[1][1]=5;My_array_variable[1][2]=6;for(int i=0;i<2;i++){for(int j=0;j<3;j++){System.out.println("the value of My_array_variable ["+i+"]["+j+"]::"+My_array_variable[i][j]);}}}

Do and See the outputGo to Slide-1

Page 20: Data types in java

• Public static void main(String[] y){ int goto=5; System.out.println(“The value of goto“+goto); }• Public static void main(String[] y){ long int 1a=5l;System.out.println(“the value of 1a is ",1a);}• Public static void main(String[] y){ int _=5; int __=10; int ___; ___=_+__; ;System.out.println(“the

value of ___”+___);}• Public static void main(String[] y){int [] demo[]=new int[5][];}