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

Java Unit-1.1

The document discusses various Java programming concepts including tokens, keywords, identifiers, literals, operators, separators, comments, data types, access modifiers, arrays. It provides definitions and examples of each concept.

Uploaded by

JAGRITI SACHDEVA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Java Unit-1.1

The document discusses various Java programming concepts including tokens, keywords, identifiers, literals, operators, separators, comments, data types, access modifiers, arrays. It provides definitions and examples of each concept.

Uploaded by

JAGRITI SACHDEVA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Lecture Objectives

In this lecture, we will discuss:


•Introduction to Java. Difference
between C++ and Java.

1
2
3
4
5
JAVA TOKENS
• The Java compiler breaks the line of code into text (words) is called Java tokens. These are
the smallest element of the Java Program. The Java compiler identified these words as
tokens.

• Types of Tokens
• Keywords
• Identifiers
• Literals
• Operators
• Separators
• Comments
JAVA KEYWORDS
Java has a set of keywords that are reserved words that cannot be used as
variables, methods, classes, or any other identifiers.
Example break, abstract, byte, boolean, etc…
true, false, and null are not keywords, but they are literals and reserved
words that cannot be used as identifiers.

Which statement is true?


Select the one correct answer.
(a) new and delete are keywords in the Java language.
(b) try, catch, and thrown are keywords in the Java language.
(c) static, unsigned, and long are keywords in the Java language.
(d) exit, class, and while are keywords in the Java language.
(e) return, static, and default are keywords in the Java language.
(f) for, while, and unsigned are keywords in the Java language.
Identifiers
• Identifiers are used to name a variable, constant, function, class, and array. It usually
defined by the user. It uses letters, underscores, or a dollar sign as the first character.
Remember that the identifier name must be different from the reserved keywords. There are
some rules to declare identifiers are:

• The first letter of an identifier must be a letter, underscore or a dollar sign. It cannot start
with digits but may contain digits.
• The whitespace cannot be included in the identifier.
• Identifiers are case sensitive.
Eg. PhoneNumber , PRICE , radius , a , a1 , _phonenumber , $circumference , jagged_array.
12radius //invalid
LITERALS
• In programming literal is a notation that represents a fixed value (constant) in the
source code. It can be categorized as an integer literal, string literal, Boolean literal, etc. It
is defined by the programmer. Once it has been defined cannot be changed. Java provides
five types of literals are as follows:
• Integer
• Floating Point
• Character
Literal Type
• String
23 int
• Boolean
9.86 double
false, true boolean
'K', '7', '-' char
"javatpoint" String
null any reference type
OPERATORS
• In programming, operators are the special symbol that tells the compiler to perform a
special operation. Java provides different types of operators that can be classified
according to the functionality they provide. There are eight types of operators in java, are as
follows:
• Arithmetic Operators Operator Symbols
• Assignment Operators Arithmetic +,-,/,*,%
• Relational Operators Unary ++ , - - , !
• Unary Operators Assignment = , += , -= , *= , /= , %= , ^=

• Logical Operators Relational ==, != , < , >, <= , >=

• Ternary Operators Logical && , ||


Ternary (Condition) ? (Statement1) :
• Bitwise Operators (Statement2);
• Shift Operators Bitwise &,|,^,~
Shift << , >> , >>>
SEPARATORS
• The separators in Java is also known as punctuators. There are nine separators in Java, are
as follows:

• Square Brackets []: It is used to define array elements. A pair of square brackets represents
the single-dimensional array, two pairs of square brackets represent the two-dimensional
array.
• Parentheses (): It is used to call the functions and parsing the parameters.
• Curly Braces {}: The curly braces denote the starting and ending of a code block.
• Comma (,): It is used to separate two values, statements, and parameters.
• Assignment Operator (=): It is used to assign a variable and constant.
• Semicolon (;): It is the symbol that can be found at end of the statements. It separates the
two statements.
• Period (.): It separates the package name form the sub-packages and class. It also separates
a variable or method from a reference variable.
COMMENTS

• Comments allow us to specify information about the program inside our Java code. Java
compiler recognizes these comments as tokens but excludes it form further processing. The Java
compiler treats comments as whitespaces. Java provides the following two types of comments:

• Line Oriented: It begins with a pair of forwarding slashes (//).


• Block-Oriented: It begins with /* and continues until it founds */.
DATA TYPES
Data types specify the different sizes and values that can be stored in the variable. There are two types
of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and
double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces and Arrays.
There are 8 types of primitive data types:
• boolean data type Data Type Default Value Default size
• byte data type boolean false 1 bit
• char data type char '\u0000' 2 byte
• short data type byte 0 1 byte
• int data type
short 0 2 byte
• long data type
int 0 4 byte
• float data type
long 0L 8 byte
• double data type
float 0.0f 4 byte
double 0.0d 8 byte
ACCESS MODIFIERS
• The access modifiers in Java specifies the accessibility or 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.
• There are four types of Java access modifiers:
1. Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
2. 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.
3. 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.
4. 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.
Access Modifier within class within package outside package outside package
by subclass only

Private Y N N N

Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y
Private Example
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}

public class Simple{


public static void main(String args[]){
A obj=new A();
System.out.println(obj.data); // COMPILER ERROR
obj.msg(); // COMPILE TIME ERROR
}
}
Default Example
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
Protected Example
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;

class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Public Example
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;

class B {
public static void main(String args[]){
A obj = new A();
obj.msg();
}
Arrays
• Normally, an array is a collection of similar type of elements which has contiguous
memory location.
• Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure where
we store similar elements. We can store only a fixed set of elements in a Java array.
• Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
Advantages
• Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
• Random access: We can get any data located at an index position.
Disadvantages
• Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size
at runtime. To solve this problem, collection framework is used in Java which grows
automatically.
• There are two types of array.
• Single Dimensional Array
• Multidimensional Array

Syntax to Declare an Array in Java
1. dataType[] arr; (or)
2. dataType []arr; (or)
3. dataType arr[];
Single Dimensional Array Example
//Java Program to illustrate how to declare, instantiate, initialize
//and traverse the Java array.
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Array using for-each loop
//Java Program to print the array elements using for-each loop
class Testarray1{
public static void main(String args[]){
int arr[]={33,3,4,5};
//printing array using for-each loop
for(int i:arr)
System.out.println(i);
}}
MULTIDIMENSIONAL ARRAY
• Syntax to Declare Multidimensional Array in Java
1. dataType[][] arrayRefVar; (or)
2. dataType [][]arrayRefVar; (or)
3. dataType arrayRefVar[][]; (or)
4. dataType []arrayRefVar[];
EXAMPLE
//Java Program to illustrate the use of multidimensional array
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
JAGGED ARRAY
• Jagged Array in Java
• If we are creating odd number of columns in a 2D array, it is known as a jagged array. In
other words, it is an array of arrays with different number of columns.
//Java Program to illustrate the jagged array
class TestJaggedArray{
public static void main(String[] args){
//declaring a 2D array with odd columns
int arr[][] = new int[3][];
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[2];
//initializing a jagged array
int count = 0;
for (int i=0; i<arr.length; i++)
for(int j=0; j<arr[i].length; j++)
arr[i][j] = count++;

//printing the data of a jagged array


for (int i=0; i<arr.length; i++){
for (int j=0; j<arr[i].length; j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();//new line
}
}

You might also like