1. Java program :
double pi;
Initialization :
char c ='x';
boolean b = true;
Program example:
package com.pack1;
public class Datatype {
public static void main(String[] args) {
System.out.println("Program starts");
int i= 10;
System.out.println(i);
System.out.println("Program Ends");
}
}
o/p :
Program starts
10
Program Ends
Note :
- Java program is written in an editor & it's saved with an extension ".java" .
- Java files are called as Source file or java file.
- Java Compiler is used to convert source file to class file.
- Class file has an intermediate language is called as byte code which is generated by java compiler.
- JRE is used to execute class file or byte code.
- JRE converts byte code to machine level language and executes the code line by line.
- Byte code is ready to run, when we execute bytecode immediately ,JRE interpreted and executes.
- If there is no byte code then each time java file should check for syntax error and it should be compile and then it should be execute.this makes execution slow.
- It supports different operating system like windows,linux.
- It's platform independent.
- Without the java compiler the java program can be execute .
- Java is case sensitive.
2.Variable in Java:
- It's a temporary place in memory which is used to store a data.
- To identify each variable we should assign an unique identifier, as called as variable name.
- Syntax: data type variableName;
3. Data type :
- Two types of data type : (i) Primitive data type (Built-in) (ii) Derived type (Customize type)
- Primitive type: 8 types of data type in java
Ex : int count;
- Byte
- short
- int
- long
- float
- double
- char
- boolean
double pi;
Initialization :
- Syntax: Varname = data;
char c ='x';
boolean b = true;
Program example:
package com.pack1;
public class Datatype {
public static void main(String[] args) {
System.out.println("Program starts");
int i= 10;
System.out.println(i);
System.out.println("Program Ends");
}
}
o/p :
Program starts
10
Program Ends
Note :
- In java there is no garbage value.
- Without initializing the value, the variable can't print.
- Based on the scope of the variables, they categories into 2 types.
- Local variable
- Global variable.
- Local variables are declared within a method.
- Variables are declared directly within a class(outside of all methods) is called as global variable.
Note:
- In java local variables must be initialised before using.If not initialised then the timeof execution it will through error.
- Initialization is mandatory for global variables
- If programmer is not initialising global variable then java initialize global variables with default value
- Within a scope the variable name must be unique.
| Data types | Default variables for global variables |
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0 |
| float | 0 |
| double | 0 |
| char | empty |
| boolean | FALSE |