Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

6 Aug 2016

Primitive Data Types in Java

Primitive Data Types are predefined by the java language and named by a keyword. There are totally eight Primitive Data Types in Java. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Therefore, by assigning different data types to variables, we can store various data types in these variables.
The following table gives the detail information about Primitive Data Types:

Primitive Data Types (Table)

DataType Default Value Size Type
byte 0 1 byte Integral Value
short 0 2 byte Integral Value
int 0 4 byte Integral Value
long 0L 8 byte Integral Value
float 0.0f 4 byte Floating Point
double 0.0d 8 byte Floating Point
char '\u0000' 2 byte Character
boolean false 1 bit Boolean

Note: \u0000 means 0 in UNICODE (Universal International Standard Character Encoding)
Read More

31 Jul 2016

Java Program using Two different Classes in Two Different File

TestEmployee1.java

public class TestEmployee1 {
public static void main(String[] args) {
Employee1 alex = new Employee1(); //creating 3 objects "alex, linda and john" of the class Employee1
Employee1 linda= new Employee1();
Employee1 john= new Employee1();

alex.salary = 10000; // assigning salary to the object "alex"
alex.bonus = 2000;

linda.salary = 12000;
linda.bonus =1000;

john.salary = 8000;
john.bonus = 3000;

alex.calculateTotalPay(); //here calculateTotalPay() is a method defined in Employee1 class
linda.calculateTotalPay(); // we r invoking that method
john.calculateTotalPay(); //will print total pay of john

}
}

Employee1.java

public class Employee1 {
double salary;
double bonus;

void calculateTotalPay() {
double totalPay = salary + bonus;
System.out.println("Total Pay = " +totalPay); //concatenation used to get totalPay
}
}

Output:
C:\Program Files\Java\jdk1.8.065\bin>javac Employee1.java
C:\Program Files\Java\jdk1.8.065\bin>javac TestEmployee1.java
C:\Program Files\Java\jdk1.8.065\bin>java TestEmployee1

Total Pay = 12000.0
Total Pay = 13000.0
Total Pay = 11000.0
Read More