0% found this document useful (0 votes)
26 views21 pages

Lec 2

Uploaded by

bhavaya65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views21 pages

Lec 2

Uploaded by

bhavaya65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Java Basics

Identifiers
• In Java, identifiers are the names used to identify variables, methods, classes,
packages, or other elements in the code.

• Rules for Java Identifiers


• Character Set:
– An identifier must begin with a letter (A-Z or a-z), a dollar sign ($), or an underscore
(_).
– Subsequent characters can include letters, digits (0-9), dollar signs, or underscores.
• No Reserved Words:
– Identifiers cannot be the same as Java keywords (e.g., class, public, static, int).
• Case Sensitivity:
– Java is case-sensitive, so myVariable and MyVariable are two different identifiers.
• No Spaces:
– Spaces are not allowed in identifiers (e.g., my Variable is invalid).
• Length:
– There is no theoretical limit to the length of an identifier, but keeping them concise
and meaningful is good practice.
• Best Practices -
• Use meaningful names for better readability. For instance:
– Variables: Use camelCase (e.g., totalAmount).
– Constants: Use uppercase letters with underscores (e.g., MAX_LIMIT).
– Classes: Use PascalCase (e.g., CustomerData).
• Avoid single-letter names unless used for temporary variables (e.g., i, j
in loops).
• Example –
// Correct usage of identifiers
public class HelloWorld {// 'HelloWorld' is an identifier for the class
public static void main(String[] args) {
int totalScore = 100; // 'totalScore' is an identifier
final int MAX_SCORE = 200; // 'MAX_SCORE' is a constant identifier

System.out.println("Total Score: " + totalScore);


}
}
Java Keywords
• Java keywords are reserved words predefined by the
Java language.
• They have special meanings and are used to perform
specific tasks in the code.
• Because they are reserved, They cannot be used for
naming variables, methods, classes, or other identifiers.
• Examples – int, float, class, static public etc.
• In total there are 52 keywords in java. Out of which
‘const’ and ‘goto’ are reserved keywords that are not
currently used in Java but are kept for future use
Java Datatypes
• Every variable has a type associated with it, that describes the
size and type of value that variable can contain. This type is
known as Data Type. Java supports the following types of Data
Types-
• Types of Java Data type –
• Data types can be categorized into 2 categories -
1. Primitive Data types
2. Non Primitive Data types
• 1. Primitive Data types -
• Java defines 8 primitive data types - byte, short, int, long, float, double,
char and boolean.
• Integer Types -
• Java support 4 integers types- byte, short, int and long all are signed
integers i.e. can store both positive and negative values.
public class Demo2
{
public static void main(String[] args)
{
byte a = 10;
short b = 20;
int c= 30;
long d = 40l;

//Note we use lower case 'l' to denote a long value

System.out.println("value of a:"+a);

/* '+' is used to concatenate something with any string


and make a new string */

System.out.println ("value of b:"+b);


System.out.println ("value of c:"+c);
System.out.println ("value of d:"+d);
}
}
• Floating Point Types -
• Java supports 2 floating point data types - float and double.
For number containing fractional part we use these data
types.

• Note -
• Larger the size of data type, larger the time required for its
manipulation. So don't use larger size data type unnecessarily
i.e. if you need to store a value 12 for example then use byte
data type in place of int or long etc to improve the execution
speed.
• Example –

public class Demo3


{
public static void main(String[] args)
{
float a = 10.123f;

//Note we use lower case 'f' to denote a float value

double b = 20.123;

//By default floating point types are double, so no need to add any postfix for double
data type.

System.out.println("value of a:"+a);
System.out.println ("value of b:"+b);
}
}
• Characters Type -
• To store character java support char data type.
In java char is 2 byte Unicode character that
defines fully international character set that
can represent all of the characters found in
most human languages.
• We can also perform arithmetic operations on
characters, char can be thought of integer
type.
Example –
public class Demo4
{
public static void main(String[] args)
{
char ch1, ch2, ch3, ch4;
ch1 = 'A';
ch2 = 'B';
ch3 = 65; // ASCII value of A
System.out.println("values of ch1, ch2 and ch3 are:"+ch1+" "+ch2+" "+ch3);
ch1++;
ch4 = ch1;
int a = ch1+ch2;
System.out.println("values of ch4 and a are:"+ch4+" "+a);
}
}
• Boolean Type -
• To support Boolean values (true or false) java provides 'boolean' data type. It
require only 1 bit to store boolean value in memory.
Note :- we cannot use 0 for false and non zero value as true in java, as in C or C+
+.
public class Demo5
{
public static void main(String[] args)
{
boolean a,b;
a = true;
b=false;
System.out.println("values of a and b are:"+a+" "+b);
}
}
• 2. Non Primitive Data types -
• There are several non primitive data types supported by java like class, interface
etc. These will be covered next.
Data Types Conversion and Casting

• In programming there can be situation when we required to change the data


type of a variable to some other data type, this process of data type conversion
is known as type conversion or type casting.

• Types of type casting -


• Type casting can be categorized into 2 categories based on whether the two
types are compatible or not –

• 1. Compatible Type conversion -


• Java perform automatic type conversion if two types are compatible, and
destination type is larger than the source type. For example a long type is large
enough to contain all valid int or short or byte type, similarly double is large
enough to hold any valid float type.
This process of storing smaller data type values into larger compatible data type
is known as widening.
byte a=10;
int b=a;
float c=b;
// Note that float is larger than integer types (byte, short,
int, long)
All numeric data types, integers (byte, short, int, long) and
floating point type (float, double) are compatible with each
other.

• 2 - Incompatible Type Conversion -


• If two types are not compatible then we can convert the
source type into destination type as follows –
• type variable1 = (type) variable2;
• Example -
• int a=10;
• byte b = (byte)a;
• short c = (short)a;
• This process of storing larger data type values
into smaller data types is known as narrowing.

Note - We can cast integers as well as floating


point types into any data type except boolean.
Example -

public class Demo


{
public static void main(String[] args)
{
byte a=10;
int b = 297;
float c = 301.1f;
char d = 'A';

/* compatible type conversion */


int e = a;
double f = c;
float g = b;
System.out.println("values of e, f and g are :"+e+" "+f+" "+g);

/* Incompatible type conversion */


byte h = (byte)b;
float i = (float)f;
System.out.println("values of h and i are :"+h+" "+i);
}
}
Output -
values of e, f and g are :10 301.1000061035156 297.0
values of h and i are :41 301.1

Note :- the value of 'h' is 41 this is because the integer value 297
is out of the rang of byte, in such case value will be reduced by
finding the modulus (reminder of division) by the byte range
(256). (When 297 is cast to byte the result is reminder of the
division of 297 by 256 i.e. 41).
• Automatic Type Promotion in Expressions Evaluation -

• While evaluating any expression containing byte, short or char operands,


java automatically promotes them into int.

The reason behind this is that as range of these data types is limited, so
while evaluating any expression there are high chances that result may go
out of range of these data types, that's why java automatically promote
them into int to avoid such conditions.
For example –
• byte a = 75;
• byte b = 30;
• int c = a*b;
• If we want the result of same data type than we have to manually type
cast them as follows –
• byte a = 10;
• byte c =(byte) a*5;
• Note :- In any expression evaluation that contains variables
of different data types, the result of evaluation will be
promoted to largest data type available in expression.
• In any expression first of all byte, short and char are
promoted to int. Then if there is a long variable than whole
expression will be promoted to long, similarly if there is
any float variable then whole expression will be promoted
to float and so on.
• Example –
• int x = 10;
• float y = 5.5f;
• float result = x + y; // x is promoted to float
• System.out.println("Result: " + result); // Outputs: 15.5
• Standard Default values -
• When we declared variables for the first time, java
automatically assign some default values to them as shown
below -

You might also like