Created: 2023-08-26 12:40
Status: #concept
Subject: Programming
Tags: Java C Data Type Java String Class Java Auto-Boxing Java Enum
Java Data Type
The Java primitives contain the same primitives as C except with the addition of
boolean
, byte
, and String
.
- Java is strongly-typed, meaning you cannot change variable data types later.
- the assignment & calculation of values undergo the same implicit C Type Conversion.
Java doesn't allow us to make assignments that lead to the truncation of Bytes in data.
- this means, we cannot assign
float
values toint
variables. - however, we can assign
byte
todouble
variables.
Sometimes, this can be overriden by explicitly typecasting a value, like doing (int) someDoubleValue
.
Primitive & Reference Data Types
Aside from the built in Primitive Types provided by the java.lang package, everything else from
String
, ArrayList
, and user-defined class type objects are Reference Types.
- this means that passing anything that isn't a primitive to methods will pass a reference to the object and will make it mutable.
int value = 10;
System.out.println(value); // 10
public class Name {
private String name;
public Name(String name) {
this.name = name;
}
}
Name luke = new Name("Luke");
System.out.println(luke); // Name@4aa298b7
Without overloading the Java Class
.toString()
method, Objects show their T@<memory_address>
on string serialization.
Auto-Boxing
When a method requires a reference type to be used, the JVM automatically calls the constructors of Reference Type classes to be used in place of its primitive counterparts.
- this works both ways, so when we pass an object reference type to a function that expects a primitive type, Java can automatically auto-box it to a primitive.
- this notably happens with java.util.ArrayList and java.util.HashMap.
int key = 2;
HashMap<Integer, Integer> hashmap = new HashMap<>();
hashmap.put(key, 10);
int value = hashmap.get(key);
System.out.println(value); // 10
However, when we try to convert a
null
reference, the JVM will throw a java.lang.reflect.InvocationTargetException
.
List of Primitive Data Types
These values are immutable and are passed by value to methods.
boolean
eithertrue
orfalse
.byte
containing 8 Bits storing the values-128
and127
.char
a 16-bit Unicode Character.short
a 16-bit value that represents a smaller integer between-32768
and32767
.integer
contains values between-2^31
and(2^31) - 1
.long
a 64-bit value containing values between-2^63
and(2^63) - 1
.float
a floating-point number that uses 32-bits.double
same asfloat
but with 64-bits.
The null
Value
The
null
value is similar to C's NULL
value which is a Reference Type value which points to "nothing".
- this denotes an object variable points to nothing.
- this means that we cannot access methods or attributes on it - otherwise, it will throw a
NullPointerException
.
Person joan = new Person("Joan Ball");
System.out.println(joan); // Joan Ball, age 0 years
Person ball = joan;
ball.growOlder();
ball.growOlder();
System.out.println(joan); // Joan Ball, age 2 years
joan = new Person("Joan B.");
System.out.println(joan); // Joan B., age 0 years
ball = null;
System.out.println(ball); // null
ball.growOlder(); // BAD: Throws a java.lang.NullPointerException
Making Arrays
We can declare
type
arrays as variables or function parameters by appending []
to the type
.
- the
type[]
is constant and cannot change. - one way have an array with objects with different functionalities is by using Polymorphism.
See Java Array for more info.
int[] numbers = {1, 2, 3, 4};
String[] names = {"Ian", "Fitz", "Sam"};
public static void main(String[] args) {
String[] names = {"Ian", "Fitz", "Sam"};
// using a for-each loop, we print each entry 'x' of the names array
for (String x : names) {
System.out.println(x);
}
}
Using ArrayList
Utility Class
See java.util.ArrayList.
Typecasting String
Values
There are
Type
Class variants of the type
primitives, like Integer
is to int
.
- they contain a
Type.valueOf(String arg)
which parsesarg
into aType
object and returns it. - there is no String Interpolation in Java, but we can imitate stdio.h Format String and Conversion Specifier behavior with
String.format(fmt, ...args)
like C.
String valueAsString = "42";
int value = Integer.valueOf(valueAsString);
System.out.println(String.format("Your number is %d", value)); // Your number is 42
String valueAsString = "42.42";
double value = Double.valueOf(valueAsString);
System.out.println(String.format("Your number is %.2f", value)); // Your number is 42.42
String t = "TRue";
String f = "Anything else";
boolean tConverted = Boolean.valueOf(t); // true
boolean fConverted = Boolean.valueOf(f); // false
We can typecast any value into a
String
by doing String.valueOf(val)
as well.
The boolean
Type
It only consists of two values:
true
or false
.
- they are also produced when using Comparison Operators.
- they are used in Conditional Statements.
- we cannot implicitly typecast any other type into a
boolean
, sowhile(some_integer)
won't work.
boolean isFun = true;
boolean aLie = (3 == 333); // evalues to: false
The String
Type
There is a built-in
String
Java Class which can be used as a type declarator.
- we can Concatenate strings together with
+
. - many primitive data types have a
Type.toString()
static
method to convert them into a string, but we can simply"" + value
and it automatically typecasts it into aString
.
public class HiAdaLovelace {
public static void main(String[] args) {
String name = "Ada Lovelace";
System.out.println("Hi " + name + "! I like the number " Integer.toString(5));
}
}