Codecademy will be down from 00:00 to 02:00 ET on November 15, 2019 for scheduled database maintenance. This will affect the website, learning environment, forums, and mobile app. We will provide updates as necessary.
Cheat Sheets are in BETA. Give us your feedback.

int Data Type

In Java, the int datatype is used to store integer values. This means that it can store all positive and negative whole numbers and zero.

int num1 = 10; // positive value int num2 = -5; // negative value int num3 = 0; // zero value int num4 = 12.5 // not allowed

boolean Data Type

In Java, the boolean primitive data type is used to store a value, which can be either true or false.

boolean result = true; boolean isMarried = false;

char Data Type

In Java, char is used to store a single character. The character must be enclosed in single quotes.

char answer = 'y';

Primitive Data Types in Java

Java’s most basic data types are known as primitive data types and are in the system by default.

The available types are as follows:

  • int
  • char
  • boolean
  • byte
  • long
  • short
  • double
  • float

null is another, but it can only ever store the value null.

int age = 28; char grade = 'A'; boolean late = true; byte b = 20; long num1 = 1234567; short no = 10; float k = (float)12.5; double pi = 3.14;

Java Strings

A String in Java is a Object that holds multiple characters. It is not a primitive datatype.

A String can be created by placing characters between a pair of double quotes (").

To compare Strings, the equals() method must be used instead of the primitive equality comparator ==.

// Creating a String variable String name = "Bob"; // The following will print "false" because strings are case-sensitive System.out.println(name.equals("bob"));

Static Typing in Java

In Java, the type of a variable is checked at compile time. This is known as static typing. It has the advantage of catching the errors at compile time rather than at execution time.

Variables must be declared with the appropriate data type or the program will not compile.

int i = 10; // type is int char ch = 'a'; // type is char j = 20; // won't compile, no type is given char name = "Lil"; // won't compile, wrong data type

Math Operations in Java

In Java, basic math operations can be applied to int, double and float data types:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulo (yields the remainder)

These operations are not supported for other data types.

int a = 100; int b = 20; int result; // some examples of basic math operations result = a + b; // result = 120 result = a - b; // result = 80 result = a * b; // result = 2000 result = a / b; // result = 5 result = a % b; // result = 0

Comparison Operators in Java

In Java, you can use comparison operators to compare two values. These are the available operators:

  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to
  • == equal to
  • != not equal to

They are supported for primitive data types and the result of a comparison is a boolean value true or false.

int a = 5; int b = 3; boolean result = a > b; // result now holds the boolean value true