Home » Java Basics (page 10)

Java Basics

Checked and Unchecked Exceptions example

In this example we shall show you how to use a checked and an unchecked exception. A checked exception is anything that is a subclass of Exception, except for RuntimeException and its subclasses. In order to use a checked and an unchecked exception we have followed the steps below: We have created a method, void checkSize(String fileName) that creates a ...

Read More »

Java Generics examples

The Motivation for Generics The simplest way to think about Java generics is thinking about a sort of a syntactic sugar that might spare you some casting operation: List<Apple> box = ...; Apple apple = box.get(0); The previous code is self-speaking: box is a reference to a List of objects of type Apple. The get method returns an Apple instance ...

Read More »

Static variables example

This is an example of how to use static variables. Static variables are variables in a class that are the same for all instances of the class. In order to see how a static variable is used in different instances of a class implies that you should: Create a class with a static variable and a non-static variable. Create two ...

Read More »

short

In this example we shall show you how to use a short type in Java. The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).  To create a variable of short type you can just use the short keyword in the variable, as described in the code ...

Read More »

long

With this example we are going to demonstrate how to use a long type in Java. The long data type is a 64-bit signed two’s complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int. In short, ...

Read More »

int

This is an example of how to use an int type in Java. The int data type is a 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason to choose something else. This data ...

Read More »

float

In this example we shall show you how to use a float type in Java. The float data type is a single-precision 32-bit IEEE 754 floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, ...

Read More »

double

With this example we are going to demonstrate how to use a double type in Java. The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. This data type should never be used for precise values, such as currency. In short, to create a variable of double ...

Read More »

char

This is an example of how to use a char type in Java. The char data type is a single 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535 inclusive). In order to create variable of char type you should type the char keyword in a variable. Let’s take ...

Read More »

byte

In this example we shall show you how to use a byte type in Java. The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They ...

Read More »