Java Non-Primitive Data Types

Non-Primitive Data Types

Non-primitive data types are called reference types because they refer to objects.

The main differences between primitive and non-primitive data types are:

  • Primitive types in Java are predefined and built into the language, while non-primitive types are created by the programmer (except for String).
  • Non-primitive types can be used to call methods to perform certain operations, whereas primitive types cannot.
  • Primitive types start with a lowercase letter (like int), while non-primitive types typically starts with an uppercase letter (like String).
  • Primitive types always hold a value, whereas non-primitive types can be null.

Examples of non-primitive types are Strings, Arrays, Classes etc. You will learn more about these in a later article. Continue reading Java Non-Primitive Data Types

Java Data Types Example

Real-Life Example

Here’s a real-life example of using different data types, to calculate and output the total cost of a number of items:

Example

// Create variables of different data types
int items = 50;
float costPerItem = 9.99f;
float totalCost = items * costPerItem;
char currency = '$';

// Print variables
System.out.println("Number of items: " + items);
System.out.println("Cost per item: " + costPerItem + currency);
System.out.println("Total cost = " + totalCost + currency);

 

Continue reading Java Data Types Example

Java Characters

Characters

The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’:

Example

char myGrade = 'B';
System.out.println(myGrade);

 

Alternatively, if you are familiar with ASCII values, you can use those to display certain characters: Continue reading Java Characters

Java Boolean Data Types

Boolean Types

Very often in programming, you will need a data type that can only have one of two values, like:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, Java has a boolean data type, which can only take the values true or false: Continue reading Java Boolean Data Types

Java Numbers

Numbers

Primitive number types are divided into two groups:

Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you should use, depends on the numeric value.

Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.

Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating point numbers). However, we will describe them all as you continue to read.


Continue reading Java Numbers

Java Data Types

Java Data Types

As explained in the previous chapter, a variable in Java must be a specified data type:

Example

int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99f;    // Floating point number
char myLetter = 'D';         // Character
boolean myBool = true;       // Boolean
String myText = "Hello";     // String

 

Continue reading Java Data Types

Java Variables – Examples

Real-Life Examples

Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types, myChar for char types, and so on). This is done to avoid confusion.

However, for a practical example of using variables, we have created a program that stores different data about a college student:

Example

// Student data
String studentName = "John Doe";
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25f;
char studentGrade = 'B';

// Print variables
System.out.println("Student name: " + studentName);
System.out.println("Student id: " + studentID);
System.out.println("Student age: " + studentAge);
System.out.println("Student fee: " + studentFee);
System.out.println("Student grade: " + studentGrade);

 

Continue reading Java Variables – Examples

Java Identifiers

Identifiers

All Java variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order to create understandable and maintainable code:

Example

// Good
int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is
int m = 60;

 

Continue reading Java Identifiers

Java Declare Multiple Variables

Declare Many Variables

To declare more than one variable of the same type, you can use a comma-separated list:

Example

Instead of writing:

int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);

You can simply write:

int x = 5, y = 6, z = 50;
System.out.println(x + y + z);

 

Continue reading Java Declare Multiple Variables

Java Print Variables

Display Variables

The println() method is often used to display variables.

To combine both text and a variable, use the + character:

Example

String name = "John";
System.out.println("Hello " + name);

 

You can also use the + character to add a variable to another variable: Continue reading Java Print Variables