0% found this document useful (0 votes)
3 views6 pages

Java

Uploaded by

ritobratabasak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Java

Uploaded by

ritobratabasak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

Integer Types

Byte

The byte data type can store whole numbers from -128 to 127. This can be used instead of int or
other integer types to save memory when you are certain that the value will be within -128 and 127:

Example

byte myNum = 100;

[Link](myNum);

Short

The short data type can store whole numbers from -32768 to 32767:

Example

short myNum = 5000;

[Link](myNum);

Int

The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our
tutorial, the int data type is the preferred data type when we create variables with a numeric value.

Example

int myNum = 100000;

[Link](myNum);

Long
The long data type can store whole numbers from -9223372036854775808 to
9223372036854775807. This is used when int is not large enough to store the value. Note that you
should end the value with an "L":

Example

long myNum = 15000000000L;

[Link](myNum);

Floating Point Types

You should use a floating point type whenever you need a number with a decimal, such as 9.99 or
3.14515.

The float and double data types can store fractional numbers. Note that you should end the value
with an "f" for floats and "d" for doubles:

Float Example

float myNum = 5.75f;

[Link](myNum);

Double Example

double myNum = 19.99d;

[Link](myNum);

Use float or double?

The precision of a floating point value indicates how many digits the value can have after the decimal
point. The precision of float is only 6-7 decimal digits, while double variables have a precision of
about 16 digits.

Therefore it is safer to use double for most calculations.

Scientific Numbers

A floating point number can also be a scientific number with an "e" to indicate the power of 10:

Example

float f1 = 35e3f;

double d1 = 12E4d;

[Link](f1);

[Link](d1);
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:

Example

boolean isJavaFun = true;

boolean isFishTasty = false;

[Link](isJavaFun); // Outputs true

[Link](isFishTasty); // Outputs false

Boolean values are mostly used for conditional testing.

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';

[Link](myGrade);

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

Example

char myVar1 = 65, myVar2 = 66, myVar3 = 67;

[Link](myVar1);

[Link](myVar2);

[Link](myVar3);

Strings

The String data type is used to store a sequence of characters (text). String values must be
surrounded by double quotes:

Example

String greeting = "Hello World";


[Link](greeting);

The String type is so much used and integrated in Java, that some call it "the special ninth type".

A String in Java is actually a non-primitive data type, because it refers to an object. The String object
has methods that are used to perform certain operations on strings.

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.

The var Keyword

The var keyword was introduced in Java 10 (released in 2018).

The var keyword lets the compiler automatically detect the type of a variable
based on the value you assign to it.

This helps you write cleaner code and avoid repeating types, especially for long
or complex types.

For example, instead of writing int x = 5;, you can write:

Example

var x = 5; // x is an int

[Link](x);When using var, the compiler understands that 5 is an int.

Example with Different Types

Here are some examples showing how var can be used to create variables of
different types, based on the values you assign:

Example
var myNum = 5; // int

var myDouble = 9.98; // double

var myChar = 'D'; // char

var myBoolean = true; // boolean

var myString = "Hello"; // String

Important Notes

1. var only works when you assign a value at the same time (you can't
declare var x; without assigning a value):

var x; // Error
var x = 5; // OK

2. Once the type is chosen, it stays the same. See example below:

var x = 5; // x is now an int


x = 10; // OK - still an int
x = 9.99; // Error - can't assign a double to an int

When to Use var

For simple variables, it's usually clearer to write the type directly
(int, double, char, etc.).

Example
// Without var

ArrayList<String> cars = new ArrayList<String>();

// With var

var cars = new ArrayList<String>();

You might also like