Variables & Data Types in Java

Variables are containers for storing data values

In Java, there are different types of variables

  • String: Stores text
  • int and long: Stores integers
  • float and double: Stores floating point numbers
  • char: Stores single characters
  • boolean: Stores values with two states: true or false

String Data Type

The String data type stores a sequence or array of characters.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		String fullName = "Kevin";
		System.out.println("full name: " + fullName);

		String email = "octopuscodes@gmail.com";
		System.out.println("email: " + email);
	}
}
full name: Kevin
email: octopuscodes@gmail.com
 

Int Data Type

The int data type stores integers (whole numbers), without decimals.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		int age = 20;
		System.out.println("age: " + age);

		int quantity = 15;
		System.out.println("quantity: " + quantity);
	}

}
age: 20
quantity: 15
 

Long Data Type

The long data type is used when a wider range than int data type is needed.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		long creditCardNumber = 5467123456321234L;
		System.out.println("creditCardNumber: " + creditCardNumber);

		long quantity = 10000;
		System.out.println("quantity: " + quantity);
	}

}
creditCardNumber: 5467123456321234
quantity: 10000

Best Monitors for Programming

Float Data Type

The float data type stores floating point numbers.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		float pi = 3.14f;
		System.out.println("pi: " + pi);
		
		float scores = 7.8f;
		System.out.println("scores: " + scores);

		float area = 13456.1574f;
		System.out.println("area: " + String.format("%.2f", area));
	}

}
pi: 3.14
scores: 7.8
area: 13456.16
 

Double Data Type

The double data type is similar to float. The difference between the two is that is double twice the float in the case of decimal precision.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		double price = 123.56;
		System.out.println("price: " + price);

		double perimeter = 56.7234;
		System.out.println("perimeter: " + perimeter);

		double area = 12456.1574;
		System.out.println("area: " + String.format("%.2f", area));
	}

}
price: 123.56
perimeter: 56.7234
area: 12456.16
 

Char Data Type

The char data type stores a single character/letter or ASCII values.

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		char key = 'H';
		System.out.println("key: " + key);

		char ch = 97;
		System.out.println("ch: " + ch);
	}

}
key: H
ch: a
 

Boolean Data Type

The boolean data type stores values with two states: true or false

package octopuscodes.com.demo;

public class Demo {

	public static void main(String[] args) {
		boolean status = true;
		System.out.println("status: " + status);

		boolean active = false;
		System.out.println("active: " + active);
	}

}
status: true
active: false