0% found this document useful (0 votes)
7 views

Data Types

JAVA data types

Uploaded by

kynley
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Data Types

JAVA data types

Uploaded by

kynley
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Types, Variables, and Operators

1. Data Types in Java


Data types specify the type of values that can be stored in a variable. Java
provides two categories of data types:
A. Primitive Data Types
Primitive data types are the most basic data types available in Java. There
are eight types of primitive data types:
Data Size (in
Description Example Values
Type bits)
byte 8-bit signed integer 8 -128 to 127
short 16-bit signed integer 16 -32,768 to 32,767
int 32-bit signed integer 32 -2^31 to 2^31-1
long 64-bit signed integer 64 -2^63 to 2^63-1
Single-precision 32-bit floating 3.4e-038 to
float 32
point 3.4e+038
Double-precision 64-bit floating 1.7e-308 to
double 64
point 1.7e+308
char Single 16-bit Unicode character 16 '\u0000' to '\uffff'
Represents one bit of 1 (not
boolean true or false
information precise)
 Example:
java
Copy code
int myAge = 25;
boolean isJavaFun = true;
char grade = 'A';
B. Non-Primitive (Reference) Data Types
These data types refer to objects and store references (or memory
addresses) to data. These include:
 String: A sequence of characters.
 Arrays: Containers that hold multiple variables of the same data type.
 Classes and Objects: Created from user-defined classes.
 Example:
java
Copy code
String name = "John";
int[] numbers = {1, 2, 3, 4, 5};
2. Type Casting
Type casting is the process of converting one data type into another. There
are two types of casting in Java:
A. Implicit Casting (Widening)
Java automatically converts a smaller data type to a larger data type, as it is
safe and will not result in data loss.
 Example:
java
Copy code
int a = 50;
long b = a; // int is automatically cast to long
B. Explicit Casting (Narrowing)
You need to manually convert a larger data type to a smaller one, as this
may result in data loss.
 Example:
java
Copy code
double x = 10.99;
int y = (int) x; // double is explicitly cast to int
3. Variables in Java
Variables are containers for storing data values. Each variable in Java has:
1. A type (determining the data it can store).
2. A name (identifier).
3. A value (data assigned to the variable).
A. Declaration and Initialization
 A variable is declared using the syntax:
java
Copy code
<data_type> <variable_name>;
 Variables can be initialized during or after declaration:
java
Copy code
int number = 10; // Declaration and initialization
B. Variable Types
Java defines three types of variables:
1. Local Variables: Declared inside methods, constructors, or blocks,
and must be initialized before use.
2. Instance Variables: Defined within a class but outside any method,
and are specific to the object created.
3. Static Variables: Defined with the static keyword in a class, but
shared among all instances of that class.
 Example:
java
Copy code
public class Person {
static String species = "Human"; // Static variable
String name; // Instance variable

public void setName(String name) {


this.name = name; // Local variable
}
}
4. Variable Scope
The scope of a variable refers to the part of the program where it can be
accessed. Java has three scopes:
 Local Scope: Variables declared within methods or blocks can only be
accessed within those methods or blocks.
 Instance Scope: Instance variables are accessible by all methods
within the class.
 Class Scope: Static variables are accessible by all instances of the
class.
5. Operators in Java
Operators are special symbols or keywords used to perform operations on
variables and values.
A. Arithmetic Operators
These operators are used to perform basic mathematical operations.
Operat Exampl
Description
or e
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
Modulus
% a%b
(remainder)
 Example:
java
Copy code
int a = 10;
int b = 3;
int sum = a + b; // sum = 13
int remainder = a % b; // remainder = 1
B. Relational (Comparison) Operators
These operators are used to compare two values and return true or false.
Operat Exampl
Description
or e
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a<b
Greater or
>= a >= b
equal
<= Less or equal a <= b
 Example:
java
Copy code
int x = 5;
int y = 8;
boolean result = x < y; // result is true
C. Logical Operators
Logical operators are used to perform logical operations on boolean values.
Operat Descripti Exampl
or on e
Logical
&& a && b
AND
` `
Logical
! !a
NOT
 Example:
java
Copy code
boolean a = true;
boolean b = false;
boolean result = a && b; // result is false
D. Assignment Operators
Assignment operators are used to assign values to variables.
Operat Exampl
Description
or e
= Assign value a=5
+= Add and assign a += 5
Subtract and
-= a -= 5
assign
Multiply and
*= a *= 5
assign
Divide and
/= a /= 5
assign
Modulus and
%= a %= 5
assign
 Example:
java
Copy code
int num = 10;
num += 5; // num = 15
E. Bitwise Operators
Bitwise operators operate on individual bits of integer types.
Operat
Description Example
or
& Bitwise AND a&b
Bitwise
` `
OR
^ Bitwise XOR a^b
~ Bitwise ~a
Operat
Description Example
or
complement
<< Left shift a << 2
>> Right shift a >> 2
6. Expressions and Statements
 Expression: A combination of variables, operators, and values that
evaluates to a single value.
o Example: a + b * c
 Statement: A complete unit of execution, such as variable
declarations, assignments, or method calls, usually ending with a
semicolon.
o Example: int x = 5;
7. Operator Precedence
Java operators follow specific precedence rules, determining the order in
which they are evaluated.
 Example:
java
Copy code
int result = 5 + 3 * 2; // result is 11, as * has higher precedence than +

You might also like