SwadhyAI Notes

All Java notes

Java data types

›Short versionOpen if you only have a minute.
  • The eight primitives are byte, short, int, long, float, double, char, and boolean. String is a reference type.
  • A plain whole number is an int. A number that does not fit in an int needs an L and the type long.
  • int arithmetic that passes the top of the range wraps to a negative value. It does not throw.
  • A decimal literal is a double. A float literal ends in f, or the compiler rejects the assignment.
  • char uses single quotes and holds one code unit. "A" is a String and will not fit in a char.

The type is a size and a set of operations, chosen when you declare the name. Pick the one that can hold the values you will actually compute, not the one that happens to look right for the first example.

java
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";

Five of those are . String is not. It is a . The distinction shows up the moment you call a method, compare two texts, or try to store null.

Whole numbers, and the wrap

is the ordinary whole number: age, a count, an index. long is the 64-bit whole number, and its literals end in L. byte holds -128 through 127. short holds -32,768 through 32,767. You reach for those two when a format or a memory layout demands them, not as a general habit.

java
int age = 25;
int temperature = -5;
long worldPopulation = 7800000000L;
byte small = 25;

7800000000 without the L does not compile. The literal is too big to be an int, and Java will not promote a literal to long because the variable on the left is a long. The suffix is the decision.

is the matching failure at run time. The type does not grow to fit the result.

java
int top = Integer.MAX_VALUE;
System.out.println(top + 1);

Decimals, characters, and flags

is the default decimal. Use it for a measurement. Do not use it for cents and then act surprised when 0.1 + 0.2 is not 0.3. A float is smaller and less precise, and the literal has to say so.

java
double price = 19.99;
double pi = 3.14159265359;
float weight = 65.5f;

A is one code unit in single quotes: char grade = 'A';. char name = "John"; does not compile, because double quotes build a String, and a String is more than one code unit anyway. boolean holds true or false and nothing else. It is not a number. if (1) is illegal, which is the point of a separate type.

The language does not promise a size in memory for boolean. What it promises is the two values. Defaults such as 0 and false apply to fields. A local still must be assigned before you read it, whichever primitive you chose.

What is wrong with float price = 19.99; char name = "John"; int temperature = 98.6;?

19.99 is a double, so the float needs 19.99f. "John" is a String, and a char can hold one character in single quotes, such as 'J'. 98.6 is a double, so an int cannot store it. A temperature with a fraction wants double, or an explicit cast if you have decided to drop the fraction.

Moving a value into a narrower type, and the truncation that comes with it, is Java type casting.