BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Data types in java
-
Upload
yugandhar-vadlamudi -
Category
Education
-
view
342 -
download
0
Transcript of Data types in java
Data Types in JavaVariablesArrays
Practice questions
Category of Data Types
• 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.
Data Types with default value and memory
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
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
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>;
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
Types of Variables in Java
• Static variables• Instance variables• Method local and Method parameters• Block variables
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
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
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(){…}}
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.
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
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
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.
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]);}}
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.
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
• 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][];}