Storing and Operating in Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Storing and operating in java

Variable

A variable is the name of a reserved area allocated in memory. In other words, it is a name of the
memory location. It is a combination of "vary + able" which means its value can be changed.

Java Data Types


Every individual bit of data that is processed every day is
categorized into types. The type of data is known as
datatype. Java uses various kinds of data types.
However, the data types are mainly of two categories:

a. Primitive Data Types- These data types are already hard


coded into the compiler to be recognized when the
program is executed. Examples are- int,float etc.
b. Non-Primitive Data Types- These data types are special
types of data which are user defined, i,e, the program
contains their definition. Some examples are- classes,
interfaces etc.

There are 8 types of Java primitive data types namely:


a. Int
b. Float
c. Char
d. Boolean
e. Byte
f. Short
g. long
h. Double.
a. Integer Datatype in Java
int is used for storing integer values. Its size is 4 bytes . It
can be used to store integer values unless there is a need
for storing numbers larger or smaller than the limits
Example- int a=56;

b. Float Datatype in Java


float is used for storing decimal values. Its default value is
0.0f and has a size of 4 bytes. It has an infinite value
range.
Example- float a=98.7f;
c. Character Datatype in Java
char as the name suggests is useful for storing single
value characters.
Example- char a=’D’;
d. Boolean Datatype in Dava
boolean is a special datatype which can have only two
values ‘true’ and ‘false’. It has a default value of ‘false’ and
a size of 1 byte.

e. Byte Datatype in Java


It’s an 8 bit signed two’s complement . It can be a
replacement for int datatype usage but it doesn’t have the
size range as the integer datatype.
Example- byte a = 10;

f. Short Datatype in Java


This datatype is also similar to the integer datatype.
However it’s 2 times smaller than the integer datatype.
Example- short a= 54;

g. Long Datatype in Java


This datatype primarily stores huge sized numeric data. It
has a size of 8 bytes and is useful when you need to store
data which is longer than int datatype.
Example- long a= 1273762;
Java Operators

Operators are used to perform operations on variables and


values.

Java divides the operators into the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Arithmetic Operators
● Arithmetic operators are used to perform common
mathematical operations.

Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y


/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable ++x


by 1

-- Decrement Decreases the value of a variable --x


by 1

A list of all assignment operators:


Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

Java Comparison Operators

Comparison operators are used to compare two values (or


variables). This is important in programming, because it
helps us to find answers and make decisions.

The return value of a comparison is either true or false.


These values are known as Boolean value
Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal x >= y


to

<= Less than or equal to x <= y

Java Logical Operators

You can also test for true or false values with logical
operators.

Logical operators are used to determine the logic between


variables or values:

Operato Name Description Example


r
&& Logical Returns true if both x < 5 && x
and statements are true < 10

|| Logical Returns true if one of x < 5 || x <


or the statements is true 4

! Logical Reverse the result, !(x < 5 &&


not returns false if the x < 10)
result is true

You might also like