0% found this document useful (0 votes)
33 views9 pages

JAVA Quick Reference

This document serves as a quick reference for commonly used data types and operators in Java, detailing their descriptions, sizes, and default values. It emphasizes the importance of understanding memory allocation for efficient programming and provides examples of various operators and their functions. The conclusion highlights the necessity of selecting the appropriate data types and operators to ensure optimal performance in Java applications.
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)
33 views9 pages

JAVA Quick Reference

This document serves as a quick reference for commonly used data types and operators in Java, detailing their descriptions, sizes, and default values. It emphasizes the importance of understanding memory allocation for efficient programming and provides examples of various operators and their functions. The conclusion highlights the necessity of selecting the appropriate data types and operators to ensure optimal performance in Java applications.
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
You are on page 1/ 9

Quick reference: Java data

types and operators


Commonly used data types and operators in Java

In this lesson you learned about the most important data types and operators in Java
and also got some practical experience in declaring and initializing them. But there are
many other data types that you will use in Java going forward. This reading serves as a
quick reference to the list of commonly used data types and operators in Java. Keep
this handy list close at hand to speed up your coding.

Notice that the table below includes a column that indicates the bit size for each data
type, representing how much memory each type occupies and how much data it can
hold. In programming, efficient memory usage is crucial, as everything in your program
consumes memory space. Understanding the bit size helps you choose the appropriate
data type based on the required range and memory constraints, ensuring optimal
performance and resource management.

Data Description Size Number Range Defau


type (Bits lt
) Value

byte 8-bit signed integer. 8 -128 to 127 0

Normally used when


memory storage is a
concern, as it only takes
up 8 bits.

short 16-bit signed integer. 16 -32,768 to 32,767 0


Can save memory
compared to int when
you need a larger range
Data Description Size Number Range Defau
type (Bits lt
) Value

than byte.

int 32-bit signed integer. 32 −2,147,483,648 to 0


Commonly used as the 2,147,483,647
default data type for
integral values.

long 64-bit signed integer. 64 −9,223,372,036,854, 0L


Used when a wider range 775,808 to
than int is needed. 9,223,372,036,854,7
75,807

float Single-precision 32-bit 32 Approximately 0.0f


floating point. Used ±3.40282347E+38F
mainly for saving memory (6-7 significant
in large arrays of floating decimal digits)
point numbers.

double Double-precision 64-bit 64 Approximately 0.0d


IE floating point. Default ±1.79769313486231
data type for decimal 570E+308 (15
values. significant decimal
digits)

char A single 16-bit Unicode 16 Any single keyboard


character. character

boolean Represents one bit of 1 true or false false


Data Description Size Number Range Defau
type (Bits lt
) Value

information, but its "size"


is not precisely defined.

String Used to store text values Uses memory as


required

Commonly used operators in Java

Operat Title Description Example Example


or stateme result
nt

+ Addition Addition operator for 5 + 3 8


operator numeric values and
"Hello " "Hello
concatenation
+ "World" World"
operator for String
values

- Subtraction Subtracts numbers 10 - 4 6


operator for
numeric
values

* Multiplication Multiplies numbers 7 * 2 14


operator for
numeric
values
Operat Title Description Example Example
or stateme result
nt

/ Division Divide numbers 20 / 4 5


operator for
21/4.0 Because an
numeric
operation
values
between an
integer value
and an integer
always results
in an integer.

5.25

Because in an
operation
when at least
one of the
values has a
decimal point,
it results in a
decimal.

% Modulus Returns the 10 % 3 1


operator for remainder of the
numeric division
values

++ Increment Increases the value int number will


operator of a variable by 1 number = be 6 but x will
5; int x be 5
Increments the value = number+
of the variable by 1 number will
+;
and then be 6 and x will
Operat Title Description Example Example
or stateme result
nt

uses/assigns it int be 6
number =
5; int x
= +
+number;

_ _ Decrement Decreases the value int number will


operator of a variable by 1 number = be 4 But x will
5; be 5
Decrements the
number will
current value of the int x =
be 4 And x
variable by 1 and number --
will also be 4
then uses/assigns it ;

int
number =
5; int x
= --
number;

= Assignment Assigns a value to a int


operator variable number =
10

+= Addition Adds the right int 8


assignment operand to the left number =
Because
operator operand and assigns 5;
number +=
the result to the left 3;
number +=
operand
3; This is the
same as
Operat Title Description Example Example
or stateme result
nt

writing:
number =
number + 3;

-= Subtraction Subtracts the right int 3


assignment operand from the left number =
Because
operator operand and assigns 5;
number -=
the result to the left
number -= 2;
operand
2;
is the same as
writing:
number =
number - 2;

*= Multiplication Multiplies the right int 10


assignment operand with the left number =
Because
operator operand and assigns 5;
number *=
the result to the left
number *= 2;
operand.
2;
is the same as
writing:
number =
number * 2;

/= Division Divides the left int 2


assignment operand by the right number =
Because
operator operand and assigns 5;
number /=
the result to the left
number /= 2;
operand
2;
is the same as
writing:
Operat Title Description Example Example
or stateme result
nt

number =
number / 2;

%= Modulus Takes modulus int 2


assignment using two operands number =
Because
operator and assigns the 5;
result to the left number %=
number %=
operand 3;
3;

is the same as
writing:

number =
number % 3;

== Equal to Checks if the values 5 == 5 true


operator of two operands are
equal and returns a
boolean value

!= Not equal to Checks if the values 5 != 3 true


operator of two operands are
not equal and
returns a boolean
value

> Greater than Checks if the value 6 > 4 true


operator of the left operand is
greater than the
value of the right
Operat Title Description Example Example
or stateme result
nt

operand and returns


a boolean value

< Less than Checks if the value 3 < 5 true


operator of the left operand is
less than the value
of the right operand
and returns a
boolean value

>= Greater than Checks if the value 7 >= 7 true


or equal to of the left operand is
operator greater than or equal
to the value of the
right operand and
returns a boolean
value

<= Less than or Checks if the value 8 <= 10 true


equal to of the left operand is
operator less than or equal to
the value of the right
operand and returns
a boolean value

&& Logical AND Returns true if both (5 > 3) true


operator operands are true && (8 >
5)
Operat Title Description Example Example
or stateme result
nt

|| Logical OR Returns true if any of (5 > 8)|| true


operator the operands is true (8 > 5)

! Logical NOT Reverses the logical !(5 > 3) false


operator state of its operand

Conclusion

Understanding how Java allocates memory for different types of variables is


fundamental to writing efficient and performant applications so that you do not allocate
more memory than required, or allocate memory less than your requirement. You can
manage memory effectively by choosing the right data types, being mindful of memory
overhead, and leveraging Java’s garbage collection. Remember that every variable and
object you create consumes memory, so always strive for efficient memory usage to
keep your applications running smoothly.

Understanding how the operators work is the key to guiding you in choosing the correct
operator for a desired operation with a certain desired outcome. The wrong operator
could possibly result in an undesired result.

So, feel free to revisit the tables above, understand the concepts, and keep them in
mind before moving on to the next item.

You might also like