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

01_3_Data_Types

The document provides a comprehensive overview of Java programming concepts, including keywords, data types, identifiers, variables, access specifiers, and type casting. It details the 51 reserved keywords in Java, explains primitive and non-primitive data types, and describes the characteristics and rules for identifiers and variables. Additionally, it covers access modifiers and type casting methods, illustrating their usage with examples.

Uploaded by

ASK 011
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)
15 views

01_3_Data_Types

The document provides a comprehensive overview of Java programming concepts, including keywords, data types, identifiers, variables, access specifiers, and type casting. It details the 51 reserved keywords in Java, explains primitive and non-primitive data types, and describes the characteristics and rules for identifiers and variables. Additionally, it covers access modifiers and type casting methods, illustrating their usage with examples.

Uploaded by

ASK 011
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/ 19

Index:

Keywords
Datatypes
Identifiers
Variables
Access Specifier
Type Casting

Java Keywords (51 reserved keywords in Java)


abstract boolean break byte case catch

char class const continue default do

double else extends final finally float

for goto if implements import instanceof

int interface long native new package

private protected public return short static

strictfp super switch synchronized this throw

throws transient try void volatile while

true false null

List of Java Keywords


abstract: Java abstract keyword is used to declare an abstract class.
An abstract class can provide the implementation of the interface.
It can have abstract and non-abstract methods.
boolean: Java boolean keyword is used to declare a variable as a boolean type.
It can hold True and False values only.
break: Java break keyword is used to break the loop or switch statement.
It breaks the current flow of the program at specified conditions.
byte: Java byte keyword is used to declare a variable that can hold 8-bit data values.
case: Java case keyword is used with the switch statements to mark blocks of text.
catch: Java catch keyword is used to catch the exceptions generated by try
statements.
It must be used after the try block only.
char: Java char keyword is used to declare a variable that can hold unsigned 16-bit
Unicode characters
class: Java class keyword is used to declare a class.
continue: Java continue keyword is used to continue the loop. It continues the current
flow of the program and skips the remaining code at the specified condition.
default: Java default keyword is used to specify the default block of code in a switch
statement.
do: Java do keyword is used in the control statement to declare a loop. It can iterate a
part of the program several times.
double: Java double keyword is used to declare a variable that can hold 64-bit
floating-point number.
else: Java else keyword is used to indicate the alternative branches in an if
statement.
enum: Java enum keyword is used to define a fixed set of constants. Enum
constructors are always private or default.
extends: Java extends keyword is used to indicate that a class is derived from
another class or interface.
final: Java final keyword is used to indicate that a variable holds a constant value. It is
used with a variable. It is used to restrict the user from updating the value of the
variable.
finally: Java finally keyword indicates a block of code in a try-catch structure. This
block is always executed whether an exception is handled or not.
float: Java float keyword is used to declare a variable that can hold a 32-bit floating-
point number.
for: Java for keyword is used to start a for loop. It is used to execute a set of
instructions/functions repeatedly when some condition becomes true. If the number of
iteration is fixed, it is recommended to use for loop.
if: Java if keyword tests the condition. It executes the if block if the condition is true.
implements: Java implements keyword is used to implement an interface.
import: Java import keyword makes classes and interfaces available and accessible to
the current source code.
instanceof: Java instanceof keyword is used to test whether the object is an instance
of the specified class or implements an interface.
int: Java int keyword is used to declare a variable that can hold a 32-bit signed
integer.
interface: Java interface keyword is used to declare an interface. It can have only
abstract methods.
long: Java long keyword is used to declare a variable that can hold a 64-bit integer.
native: Java native keyword is used to specify that a method is implemented in native
code using JNI (Java Native Interface).
new: Java new keyword is used to create new objects.
null: Java null keyword is used to indicate that a reference does not refer to anything.
It removes the garbage value.
package: Java package keyword is used to declare a Java package that includes the
classes.
private: Java private keyword is an access modifier. It is used to indicate that a
method or variable may be accessed only in the class in which it is declared.
protected: Java protected keyword is an access modifier. It can be accessible within
the package and outside the package but through inheritance only. It can't be applied
with the class.
public: Java public keyword is an access modifier. It is used to indicate that an item is
accessible anywhere. It has the widest scope among all other modifiers.
return: Java return keyword is used to return from a method when its execution is
complete.
short: Java short keyword is used to declare a variable that can hold a 16-bit integer.
static: Java static keyword is used to indicate that a variable or method is a class
method. The static keyword in Java is mainly used for memory management.
strictfp: Java strictfp is used to restrict the floating-point calculations to ensure
portability.
super: Java super keyword is a reference variable that is used to refer to parent class
objects. It can be used to invoke the immediate parent class method.
switch: The Java switch keyword contains a switch statement that executes code
based on test value. The switch statement tests the equality of a variable against
multiple values.
synchronized: Java synchronized keyword is used to specify the critical sections or
methods in multithreaded code.
this: Java this keyword can be used to refer the current object in a method or
constructor.
throw: The Java throw keyword is used to explicitly throw an exception. The throw
keyword is mainly used to throw custom exceptions. It is followed by an instance.
throws: The Java throws keyword is used to declare an exception. Checked
exceptions can be propagated with throws.
transient: Java transient keyword is used in serialization. If you define any data
member as transient, it will not be serialized.
try: Java try keyword is used to start a block of code that will be tested for exceptions.
The try block must be followed by either catch or finally block.
void: Java void keyword is used to specify that a method does not have a return
value.
volatile: Java volatile keyword is used to indicate that a variable may change
asynchronously.
while: Java while keyword is used to start a while loop. This loop iterates a part of the
program several times. If the number of iteration is not fixed, it is recommended to
use the while loop.

Data Types in Java


Primitive data types 8 types: boolean, char, byte, short, int, long, float and
double.
Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.

Data Type Default Size


Value

boolean false 1 bit

char '\u0000' 2 byte single 16-bit Unicode character.


0 ('\u0000') to 65535 ('\uffff')

byte (two's complement integer) 0 1 byte -128 to 127 (inclusive)

short (two's complement integer) 0 2 byte -32,768 to 32,767 (inclusive)

int (two's complement integer) 0 4 byte -214 748 3648 to 214 748 3647 (inclusive)

long (two's complement integer) 0L 8 byte -9223372036854775808 to


9223372036854775807 (inclusive)

float (7decimal places) 0.0f 4 byte single-precision 32-bit IEEE 754 floating
point.
1.4e-45f to 3.4e+38f

double (15decimal places) 0.0d 8 byte double-precision 64-bit IEEE 754 floating
point
4.9e-324d to 1.7e+308d

float/double: Value range unlimited


Boolean Data Type
store only two possible values: true and false.

 1 bit

 default value false.

Unicode System
Universal international standard character encoding that is capable of
representing most of the world's written languages

Why java uses Unicode System?


In unicode, character holds 2 byte, so java also uses 2 byte for characters.

lowest value:\u0000
highest value:\uFFFF

Reference Data Types:


Class: Objects of user-defined classes.
Interface: References to objects of classes that implement a specific interface.
Array: References to arrays.
Enumeration (enum): A special data type for defining a set of constants.
Annotation :A special data type used for metadata about other program elements.
String: A special class in Java used to represent and manipulate strings of characters.
Primitive data types reference data types
 store the actual values  store references or addresses to
 used for basic data storage and objects in memory
calculations.  used for complex data structures and
objects that you create

Wrapper classes: These classes provide a way to use primitive data types as objects.
For example, Integer for int, Double for double, and so on.

Identifiers
 All Java variables must be identified with unique names.
 Identifiers must start with a letter, $ ,underscore ( _ ) but cannot start with a
number.
 Can't use a Java keyword as an identifier.
 Are Case sensitive (Sab Chalega)
 characters [A-Z] or [a-z] or numbers [0-9], and underscore(_) or a dollar sign ($).
 There should not be any space in an identifier.
 An identifier should not be any query language keywords such as SELECT, FROM,
COUNT, DELETE, etc.

Valid identifiers: Invalid identifiers:


TestVariable
testvariable @javatpoint
a java tpoint
i 123javatpoint
Test_Variable
_testvariable
$testvariable
sum_of_array
TESTVARIABLE
jtp123
JavaTpoint
Javatpoint123

Java Variables
A variable is the name of a reserved area allocated in memory. In other words,
it is a name of the memory location.
Memory is associated to data type and can assign value.
There are three types of variables in java: local, instance and static.
Should not start with the special characters like & (ampersand), $ (dollar), _
(underscore).
1) Local Variable
A variable declared inside the body of the method is called local variable.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared
among instances.

3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You
can create a single copy of the static variable and share it among all the instances of
the class. Memory allocation for static variables happens only once when the class is
loaded in the memory.
Initialization of a variable with a
primary value
int n1; //declare
n1 =21 ; // assignment
int i2 = 18; // initialization
char ch = ‘S’; // initialization
double d = 21.8; // initialization
d = n1; // assignment
float f1 = 16.13F; //initialization

Access Specifier (access modifiers and non-access modifiers)

access modifiers:
scope of a field, method, constructor, or class.
We can change the access level of fields, constructors, methods, and class by
applying the access modifier on it.
Private:
The access level of a private modifier is only within the class.
It cannot be accessed from outside the class.
Default:
The access level of a default modifier is only within the package.
It cannot be accessed from outside the package.
If you do not specify any access level, it will be the default.
Protected:
The access level of a protected modifier is within the package and outside the
package through child class.
If you do not make the child class, it cannot be accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
non-access modifiers, such as static, abstract, synchronized, native, volatile,
transient, etc.
Here, we are going to learn the access modifiers only.

Access within within outside package by outside


Modifier class package subclass only package

Private Y N N N

Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y

1) Private (within the class)

A class contains private data member and


private method.
We are accessing these private members
from outside the class, so there is a compile-
time error.

A obj = new A();


creating an object of class A
Private within a class
Private within package

Error

2) Default (within class, package)

If you don't use any modifier, it is treated as default by default.


In this example, we have created two packages pack and mypack. We are accessing
the A class from outside its package, since A class is not public, so it cannot be
accessed from outside the package.
3) Protected (within class, within package, outside package by subclass)
The protected access modifier is accessible within package and outside the
package but through inheritance only.
The protected access modifier can be applied on the data member, method and
constructor. It can't be applied on the class.
It provides more accessibility than the default modifer.
In this example, we have created the two packages pack and mypack. The A class of
pack package is public, so can be accessed from outside the package. But msg
method of this package is declared as protected, so it can be accessed from outside
the class only through inheritance.

4) Public
The public access modifier is accessible everywhere. It has the widest scope among all other
modifiers.
(within class, within package, outside package by subclass, outside package)

Type Casting(Narrowing)
Output:
10.5
10

Java Variable Example: Overflow

Output:
130
-126

Java Variable Example: Adding Lower Type


Any arithmetic operation involving byte, short --- automatically promoted to int.
Output:
20

Java Type Casting


Type casting is when you assign a value of one primitive data type to another type.
two types of casting:

1. Widening Casting implicitly (automatically)


 Converting a lower data type into a higher one is called widening type casting.
 known as implicit conversion or casting down.
byte -> short -> char -> int -> long -> float -> double

Narrowing Casting explicate (manually) –

 Converting a higher data type into a lower one is called narrowing type casting.
 It is also known as explicit conversion or casting up.

double -> float -> long -> int -> char -> short -> byte
Assignment

int a = 1, b = 2, c = 5;
a=b=c;
a = b = c;
1) b=c //b=5
System.out.print("a= " + a + " b= " + b + " c= " + c);
2) a=b //a=5
so
o/p: a=5 b=5 c=5 a=5,b=5,c=5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy