JAVA VARIABLES- TYPES DATA TYPES 1Prepared by P.PRATHIBHA
Topics for Today’s Session About JAVA JVM DATA TYPES Variables Type Casting Types of Variables
AboutJava  Java is a computing platform for application development and an object-oriented,  Java is Class-based and Concurrent programming language   It means the code can be executed by multiple processes at the same time.  Java can run on all platforms and free to access.  Java is Simple, Secure, Robust, Complete Object oriented and Platform Independent High level Language  It is Portable and Multi-thread technology gives High Performance.
 JAVA VIRTUAL MACHINE    (JVM) vJava compiler produce an intermediatry code known as byte code for a machine. vThis machine is called the JVM, it exist only inside the computer memory. v The process of compiling a java program into byte code is referred to as Virtual Machine code.
v The virtual machine code is not machine specific. v The machine code is generated by the Java Interpreter, by acting as an intermediary between the virtual machine and the real machine.
Variables v Variable is an identifier, which denotes a storage location used to store a data value v A variable is a container which holds the value while the Java program is executed. v A variable is assigned with a data type. v Variable is a name of memory location. v It is a combination of vary + able that means its value can be changed. v Declaring (Creating) Variables Syntax: type variable = value; Where type is one of Java's types (such as int or String), and  variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable. Example: String name = John; char ch = 'A'; int number = 100;
Variable Naming conventions 1) Variable names may consists letters, digits, _(underscore)   dollar ($). 2)  Variables naming cannot contain white spaces,    for example: int num ber = 100; // is invalid       because the variable name has space in it. 3) Variable name can begin with special characters such as $  and _ 4)  As per the java coding standards the variable name should  begin with a lower case letter,  for example: int num;  5) Variable names are case sensitive in Java. 6) It should not be a Keyword. 7)  Variable names can be of any length.
LocalVariables InstanceVariables StaticVariables Types / Scope of Variables v There are three types of variables in java:
1. Local Variable  A variable defined within a block or method or constructor  is called local variable.  The scope of these variables exists only within the block in  which the variable is declared. i.e. we can access these  variable only within that block.  A local variable cannot be defined with static keyword.  These variable are created when the block in entered or the  function is called and destroyed after exiting from the block  or when the call returns from the function.  Initialization of Local Variable is Mandatory.
2. Instance Variable v Instance variables are non-static variables. v A variable declared inside the class but outside the body of  the method, Constructor or Block.  v These  variables are created when an  object  of  the  class  is  created and destroyed when the object is destroyed. v Each instance(objects) of class has its own copy of instance  variable. v It  is  called  instance  variable  because  its  value  is  instance  specific and is not shared among instances. v Initialization  of  Instance  Variable  is  not  Mandatory.  Its  default value is 0. v Instance Variable can be accessed only by creating objects.
3. Static Variables: Ø Static variables are also known as Class variables. Ø Static variables are declared using the static keyword within a class, outside any method constructor or block. Ø It cannot be local. Ø You can create a single copy of static variable and share among all the instances of the class. Ø Memory allocation for static variable happens only once when the class is loaded in the memory.  Static variables are created at the start of program execution and destroyed automatically when execution ends.  Initialization of Static Variable is not Mandatory. Its default value is zero (0).  Static variables can be accessed by class name, not by object. For example, If I create 4 objects of a class and access this static variable, it would be common for all, the changes made to the variable using one of the object would reflect when you access it through other objects.
Example: Using variables class A {   int  d=30; //instance variable   static int m=100; //static variable   void method() {   int  m=20; //local variable   }   } //end of class  
Type Casting Ø Type casting is nothing but assigning a value of one primitive data type to another. Ø When you assign the value of one data type to another, you should be aware of the compatibility of the data type. Ø If they are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion  Ø And if not, then they need to be casted or converted explicitly. Syntax: Ø Four integer types can be cast to any other type except Boolean. Ø Casting into a smaller type may result in a loss of data. Ø Similarly, the float double can be cast to any other type except Boolean. Ø There are two types of casting in Java : type variable1= (type) variable
1. Widening Casting (automatically) – converting a smaller type to a larger type size, this type of conversion is called Implicit conversion. 2. Narrowing Casting (manually) – converting a larger type to a smaller size type, this type of conversion is Explicit conversion. From To byte short, char, int, long, float, double short int, long, float, double char int, long, float, double int long, float, double long float, double float double Example: int i = 258;  long l = i; //automatic type conversion float f = l; //automatic type conversion double d = 345.06;   long l = (long)d; //explicit type casting int i = (int)l; //explicit type casting
Data Types in Java  Data type defines the values that a variable can take  Data types specify the different sizes and values that can be stored in the variable.  In java, data types are classified into 2 categories : 1. Primitive Data type 2. Non-Primitive Data type
Primitive data types  Primitive data types are the building blocks of data manipulation.  These are the most basic data types available in Java.  Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.  There are 8 types of primitive data types: 1. boolean data type 2. byte data type 3. char data type 4. short data type 5. int data type 6. long data type 7. float data type 8. double data type
Data Type Size Description Default byte 1 byte Stores whole numbers from -128  to 127 0 short 2 bytes Stores whole numbers from - 32,768 to 32,767 0 int 4 bytes Stores whole numbers from - 2,147,483,648 to 2,147,483,647 0 long 8 bytes Stores whole numbers from - 9,223,372,036,854,775,808 to  9,223,372,036,854,775,807 0L float 4 bytes Stores fractional numbers.  Sufficient for storing 6 to 7  decimal digits 0.0f double 8 bytes Stores fractional numbers.  Sufficient for storing 15 decimal  digits 0.0d boolean 1 bit Stores true or false values false char 2 bytes Stores a single character/letter  or ASCII values ‘u0000’
Non-Primitive Data Types  Non-Primitive data types refer to objects and hence they are called reference types.  Non-primitive types include Strings, Arrays, Classes, Interface, etc. Strings Arrays Interfaces Classes Non- Primitive Data types
Strings: String is a sequence of characters.  used to store consecutive characters or proper  sentences   But in Java, a string is an object that represents a  sequence of characters.   The java.lang.String class is used to create a string  object.  Example:  String abc = “Satwika likes music; Arrays:  Arrays in Java are homogeneous data structures implemented in Java as objects. Ø Arrays store one or more values o f a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index.  It can store any type of data as the size of the array is also declared by the programmer.
Classes:  Classes are used to create objects  A class in Java is a blueprint which includes all your  data.    A class contains fields(variables) and methods to  describe the behavior of an object. Interface:  An interface is like a dashboard or control  panel for a class.  Ø It has buttons/functions for the data types  defined, but the implementation is somewhere  else. Ø  Like a class, an interface can have methods and  variables, but the methods declared  in interface are by default abstract (only method  signature, no body).
Difference between primitive and non-primitive data types  Primitive  types  are  predefined.  Non-primitive  types  are  created by the programmer and is not defined by Java (except  for String).  Non-primitive types  can  be  used  to  call  methods  to  perform  certain operations, while primitive types cannot.  A primitive type has always a value, while non-primitive types  can be null.  A  primitive  type  starts  with  a  lowercase  letter,  while  non- primitive types starts with an uppercase letter.  The size of a primitive type depends on the data type, while  non-primitive types have all the same size.
Summary  In this lesson you learnt about  About Java?  Java Virtual Machine  Variables  Types / Scope of variables  Type Casting  Data Types
Java data types, variables and jvm

Java data types, variables and jvm