top of page

Data Types

Here you will see all the data types in Java. You will see the data type int, double, boolean, char, and String.

Below you will see an Example of all the data types.

wixC1.png
Capture.PNG

int - stores integers (whole numbers), without decimals, such as 123 or -123.

double - stores floating point numbers, with decimals, such as 1.78 or -2.5.

boolean - stores values with two states: true or false.
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes.

String - stores text, such as "Hello". String values are surrounded by double quotes.

​

Capture.PNG

Here above you will see that output that I create from using all the data variables. 

The System.out.println(); in the code is there to print out the output.

Primitive Vs Reference Types

1

Primitive

  • Can never store null

  • Stores values

  • "int i = 11", i will copy 11

  • Need to use (==) to compare values to each other

2

Reference 

  • Can store null

  • Stores handle objects

  • Object obj = SomeObject         obj will point to the reference of SomeObject.

  • Need to use (.equals()) to compare the values to each other

Here on your left you will see what values each Primitive Data Types can hold up to. Click on the Button to see the website I got this from

Capture.PNG

Strings 

There are two types of Strings. A String object and a String Literal.

String Literals: 

-Stored in the String pool

String Objects: 

-Given an exact reference location, NOT stored in a String pool.

​

Things to know: 

Strings are immutable. Can go in and grab things from Strings but we won’t change them in terms of their composition. We would put a link to something else.

Dotted Background

Strings

Capture.PNG

Math Methods

  • The Math.max(x,y) method can be used to find the highest value of x and y

  • The Math.min(x,y) method can be used to find the lowest value of x and y

  • The Math.sqrt(x) method returns the square root of x

  • The Math.abs(x) method returns the absolute (positive) value of x

  • Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive)  

    • In the example it is between 1 to 10 because multiplying by 10 and adding 1.​

bottom of page