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

App Unit-2 (First Half)

Uploaded by

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

App Unit-2 (First Half)

Uploaded by

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

UNIT-2 - JAVA PROGRAMMING

PARADIGMS
Syllabus
Object and Classes; Constructor; Data types; Variables; Modifier and
Operators - Structural Programming Paradigm: Branching, Iteration,
Decision making, and Arrays - Procedural Programming
Paradigm:Characteristics; Function Definition; Function Declaration
and Calling; Function Arguments - Object-Oriented Programming
Paradigm: Abstraction; Encapsulation; Inheritance; Polymorphism;
Overriding -Interfaces: Declaring, Implementing; Extended and Tagging
- Package: Package Creation.
What is Java?
Java is a high level, robust, object-oriented and secure programming language
Application
1.Desktop Applications such as acrobat reader, media player, antivirus, etc.
2.Web Applications such as irctc.co.in, javatpoint.com, etc.
3.Enterprise Applications such as banking applications.
4.Mobile
5.Embedded System
6.Smart Card
7.Robotics
8.Games, etc.
Types of Java Applications
1) Standalone Application
Standalone applications are also known as desktop applications or window-based applications. These are
traditional software that we need to install on every machine. Examples of standalone application are
Media player, antivirus, etc. AWT and Swing are used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is called a web application.
Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for creating web
applications in Java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is called an enterprise
application. It has advantages like high-level security, load balancing, and clustering. In Java, EJB is
used for creating enterprise applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile application. Currently, Android and
Java ME are used for creating mobile applications.
Java Platforms / Editions

 1) Java SE (Java Standard Edition)


 It is a Java programming platform. It includes Java programming APIs such as java.lang, java.io,
java.net, java.util, java.sql, java.math etc. It includes core topics like OOPs, String, Regex,
Exception, Inner classes, Multithreading, I/O Stream, Networking, AWT, Swing, Reflection,
Collection, etc.
 2) Java EE (Java Enterprise Edition)
 It is an enterprise platform that is mainly used to develop web and enterprise applications. It is
built on top of the Java SE platform. It includes topics like Servlet, JSP, Web Services, EJB, JPA,
etc.
 3) Java ME (Java Micro Edition)
 It is a micro platform that is dedicated to mobile applications.
 4) JavaFX
 It is used to develop rich internet applications. It uses a lightweight user interface API.
Structure of Java Program
Structure of Java Program
Documentation Section
 The documentation section is optional for a Java program. It includes basic information about a Java
program, the author's name, date of creation, version, program name, company name, and description
of the program. It improves the readability of the program. Whatever we write in the documentation
section, the Java compiler ignores the statements during the execution of the program. To write the
statements in the documentation section, we use comments.
 Single-line Comment: It starts with a pair of forwarding slash (//). For example://First Java Program
 Multi-line Comment: It starts with a /* and ends with */. We write between these two symbols. For

example:· /*It is an example of multiline comment*/


 Documentation Comment: It starts with the delimiter (/**) and ends with */. For
example:/**It is an example of documentation comment*/
 Package Declaration
we declare the package name in which the class is placed.There can be only one package statement in a
Java program.It is necessary because a Java class can be placed in different packages and directories
based on the module they are used. For all these classes package belongs to a single parent directory.
We use the keyword package to declare the package name. For example:
 1.package javaprogram; //where javaprogram is the package name
 2.package com.javaprogram; //where com is the root directory and javaprogram is the subdirectory
CONTD...
Import Statements
 The package contains the many predefined classes and interfaces. If we want to use any class of a particular package, we need to import that
class. The import statement represents the class stored in the other package. We use the import keyword to import the class. For example:
1.import java.util.Scanner; //it imports the Scanner class only
2.import java.util.*; //it imports all the class of the java.util package
Interface Section
 It is an optional section. We can create an interface in this section if required. We use the interface keyword to create an interface. An
interface is a slightly different from the class. It contains only constants and method declarations. Another difference is that it cannot be
instantiated. We can use interface in classes by using the implements keyword. An interface can also be used with other interfaces by using
the extends keyword. For example:
interface car
{
void start();
void stop();
}
◼ Class Variables and Constants
class Student //class definition {
String sname; //variable
int id;
double percentage; }
Main Method Class

 The execution of all Java programs starts from the main() method. In other words, it is an entry point of the class. It must be inside the class.
Inside the main method, we create objects and call the methods. We use the following statement to define the main() method:
 public static void main(String args[]) {
}
For example:public class Student //class definition {
public static void main(String args[]) {
//statements
} }
Methods and behavior
 We define the functionality of the program by using the methods. The methods are the set of instructions that we want to perform. These
instructions execute at runtime and perform the specified task. For example:
public class Demo //class definition {
public static void main(String args[]) {
void display() {
System.out.println("Welcome to java");
} statements
} }
Object
➢ An object is a real-world entity.
➢ An object is a runtime entity.

➢ The object is an entity which has state and behavior.

➢ The object is an instance of a class.

An object has three characteristics:


State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of an object such as deposit,
withdraw, etc.
Identity: An object identity is typically implemented via a unique ID. The value of
the ID is not visible to the external user. However, it is used internally by the JVM
to identify each object uniquely
class in Java
 A class is a group of objects which have common properties. It is a template
or blueprint from which objects are created. It is a logical entity. It can't be
physical.
 A class in Java can contain:
❖ Fields
❖ Constructors
❖ Blocks
❖ Nested class and interface
Syntax to declare a class:

class <class_name>{
field;
method;
}
Object and Class Example
main within the class main outside the class

//Defining a Student class. //Creating Student class.


class Student{ class Student{
//defining fields int id;
int id;//field or data member or instance variable String name;
String name; }
//creating main method inside the Student class //Creating another class TestStudent1 which contains the main method
public static void main(String args[]){ class TestStudent1{
//Creating an object or instance public static void main(String args[]){
Student s1=new Student();//creating an object of Student Student s1=new Student();
//Printing values of the object System.out.println(s1.id);
System.out.println(s1.id);//access .member through ref. variable System.out.println(s1.name);
System.out.println(s1.name); }
} }
} Output:
Output: 0 0
null null
3 Ways to initialize object
1.By reference variable
2.By method
3.By constructor
Initialization through reference Initialization through method
TestStudent2.java TestStudent4.java
class Student{ class Student{
int id; int rollno;
String name;
String name;
}
void insertRecord(int r, String n){
class TestStudent2{
rollno=r;
public static void main(String args[]){
Student s1=new Student();
name=n; }
s1.id=101; void displayInformation(){System.out.println(rollno+" "+na
me);} }
s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name);//printing members with a white space
class TestStudent4{
} public static void main(String args[]){
} Student s1=new Student();
Output: Student s2=new Student();
Output:
101 Sonoo s1.insertRecord(111,"Karan"); 111 Karan
s2.insertRecord(222,"Aryan"); 222 Aryan
s1.displayInformation();
s2.displayInformation(); } }
Java Variables
 Variable is a name of memory location. There are three types of variables in java: local, instance and
static.
1) Local Variable
 A variable declared inside the body of the method is called local variable. You can use this variable
only within that method 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.
Example : Types of variables in java
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class
Constructors in Java
➢ constructor is a block of codes similar to the method.
➢ called when an instance of the class is created.
➢ At the time of calling constructor, memory for the object is allocated in the memory.
➢ It is a special type of method which is used to initialize the object.
➢ Every time an object is created using the new() keyword, at least one constructor is called.
➢ It calls a default constructor if there is no constructor available in the class. In such case,
Java compiler provides a default constructor by default.
 There are two types of constructors in Java: no-arg constructor, and parameterized
constructor.
 Rules for creating Java constructor
1.Constructor name must be the same as its class name
2.A Constructor must have no explicit return type
3.A Java constructor cannot be abstract, static, final, and synchronized
Types of Java constructors
1.Default constructor (no-arg constructor)
2.Parameterized constructor
Java Default Constructor
 A constructor is called "Default Constructor" when it doesn't have any
parameter.
Syntax of default constructor:
<class_name>(){}
Java Parameterized Constructor
➢ A constructor which has a specific number of parameters is called a
parameterized constructor.
➢ The parameterized constructor is used to provide different values to
distinct objects.
Example of default constructor
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Output:
Bike is created
Example of parameterized constructor
//Java Program to demonstrate the use of the paramete
rized constructor.
class Student4{ public static void main(String args[]){
int id; //creating objects and passing values
String name; Student4 s1 = new Student4(111,"Karan");
//creating a parameterized constructor Student4 s2 = new Student4(222,"Aryan");
Student4(int i,String n) //calling method to display the values of object
{ s1.display();
id = i; s2.display();
name = n; }
} }
//method to display the values
Output:
void display()
111 Karan
{
222 Aryan
System.out.println(id+" "+name);}
Constructor Overloading in Java

Constructor overloading in Java is a technique of having more than one constructor with
different parameter lists. They are arranged in a way that each constructor performs a different task.
//Java program to overload constructors void display(){System.out.println(id+" "+name+"
class Student5{ "+age);}
int id;
String name; public static void main(String args[]){
int age; Student5 s1 = new Student5(111,"Karan");
//creating two arg constructor Student5 s2 = new Student5(222,"Aryan",25);
Student5(int i,String n){ s1.display();
id = i; s2.display();
name = n; }
} }
//creating three arg constructor Output:
Student5(int i,String n,int a){ 111 Karan 0
id = i; 222 Aryan 25
name = n;
age=a;
}
Data Types in Java

 Data types specify the different


sizes and values that can be
stored in the variable.
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.
Access Modifiers in Java
 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.
Private
The private access modifier is accessible only within the class.

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);//Compile Time Error
obj.msg();//Compile Time Error
}
}
Example of default access modifier
//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[]){
void display()
{System.out.println("Hello");}
A obj = new A();//Compile Time Error
B s1 =new B();
s1.display();
obj.msg();//Compile Time Error
} }
Example of protected access modifier
//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();
}
}
Output:Hello
Example of public access modifier
//save by A.java
package pack;
public class A{
public 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();
}
}
Output:Hello
Operators in Java

 Operator in Java is a symbol that is used to perform operations. For example: +,


-, *, / etc.
 There are many types of operators in Java which are given below:
 Unary Operator,
 Arithmetic Operator,
 Shift Operator,
 Relational Operator,
 Bitwise Operator,
 Logical Operator,
 Ternary Operator and
 Assignment Operator.
Java Operator Precedence

Operator Type Category Precedence


postfix expr++ expr--
Unary
prefix ++expr --expr +expr -expr ~ !
multiplicative */%
Arithmetic
additive +-
Shift shift << >> >>>
comparison < > <= >= instanceof
Relational
equality == !=
bitwise AND &
Bitwise bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
Logical
logical OR ||
Ternary ternary ?:
= += -= *= /= %= &= ^= |=
Assignment assignment
<<= >>= >>>=
Java Unary Operator

 The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:
 incrementing/decrementing a value by one
 negating an expression
Output:
 inverting the value of a boolean
10
Java Unary Operator Example: ++ and -- 12
public class OperatorExample{ 12
10
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
Java Arithmetic Operators
 Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as
basic mathematical operations.
Java Arithmetic Operator Example
public class OperatorExample{
public static void main(String args[]){
int a=10; Output:
int b=5; 15
System.out.println(a+b);//15 5
50
System.out.println(a-b);//5 2
System.out.println(a*b);//50 0
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
Java Left Shift Operator

 The Java left shift operator << is used to shift all of the bits in a value to the left
side of a specified number of times.
Java Left Shift Operator Example
public class OperatorExample
{
public static void main(String args[])
{
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
}
}
Output:
40
80
Java Right Shift Operator

 The Java right shift operator >> is used to move the value of the left
operand to right by the number of bits specified by the right operand.
Java Right Shift Operator Example
public OperatorExample{
public static void main(String args[]) Output:
2
{ 5
System.out.println(10>>2);//10/2^2=10/4=2 2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}
}
Java AND Operator Example: Logical && and
Bitwise &
 The logical && operator doesn't check the second condition if the first condition is false. It
checks the second condition only if the first one is true.
 The bitwise & operator always checks both conditions whether first condition is true or
false.
public class OperatorExample{
public static void main(String args[])
{ Output:
int a=10; False
false
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}
}
Java OR Operator Example: Logical || and Bitwise |

 The logical || operator doesn't check the second condition if the first condition is true. It
checks the second condition only if the first one is false.
 The bitwise | operator always checks both conditions whether first condition is true or false.

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=5;
Output:
int c=20; True
System.out.println(a>b||a<c);//true || true = true true
System.out.println(a>b|a<c);//true | true = true
}
}
Java Ternary Operator

 Java Ternary operator is used as one line replacement for if-then-else statement and used a
lot in Java programming. It is the only conditional operator which takes three operands.
Java Ternary Operator Example
public class OperatorExample
{
public static void main(String args[])
{ Output:
2
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}
Java Assignment Operator
 Java assignment operator is one of the most common operators. It is used to assign the value on its right to
the operand on its left.
Java Assignment Operator Example
public class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3 Output:
System.out.println(a); 13
a-=4;//13-4 9
System.out.println(a); 18
a*=2;//9*2 9
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
STRUCTURAL PROGRAMMING
PARADIGM

Branching, Iteration, Decision making, and Arrays


Java Control Statements | Control Flow in Java

 Java provides three types of control flow statements.


➢ Decision Making statements
➢ if statements
➢ switch statement

➢ Loop statements
➢ do while loop
➢ while loop
➢ for loop
➢ for-each loop

➢ Jump statements
➢ break statement
➢ continue statement
Decision-Making statements:

 Decision-making statements evaluate the Boolean expression and


control the program flow depending upon the result of the condition
provided.
 There are two types of decision-making statements in Java, i.e., If
statement and switch statement
1) Simple if statement

 It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax of if statement is given below.
if(condition) {
statement 1; //executes when condition is true
}
Output:
Student.java
x + y is greater
public class Student {
than 20
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
if-else statement
 Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Student.java
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20
3) if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple else-if statements
Student.java
public class Student {
public static void main(String[] args) {
String city = "Delhi";
Output:
if(city == "Meerut") {
Delhi
System.out.println("city is meerut");
}else if (city == "Noida") {
System.out.println("city is noida");
}else if(city == "Agra") {
System.out.println("city is agra");
}else {
System.out.println(city);
}
}
}
Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement inside another if
or else-if statement.
Syntax of Nested if-statement.
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}
Switch Statement
 The switch statement contains multiple blocks of code called cases
and a single case is executed based on the variable which is being
switched. The switch statement is easier to use instead of if-else-if
statements. It also enhances the readability of the program.
 Cases cannot be duplicate
 Default statement is executed when any of the case doesn't match
the value of expression. It is optional.
 Break statement terminates the switch block when the condition is
satisfied.It is optional, if not used, next case is executed.
The syntax to use the switch statement
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
Example switch statement
public class Student implements Cloneable {
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0"); Output:
break; 2
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
}
}
}
Loop Statements

 In programming, sometimes we need to execute the block


of code repeatedly while some condition evaluates to true.
In Java, we have three types of loops that execute
similarly.
➢ forloop
➢ while loop

➢ do-while loop
Java for loop
 It enables us to initialize the loop variable, check the condition, and increment/decrement in a
single line of code. We use the for loop only when we exactly know the number of times, we want
to execute the block of code.
for(initialization, condition, increment/decrement) {
//block of statements
}

Calculation.java
public class Calculattion {
public static void main(String[] args) {
int sum = 0; Output:
for(int j = 1; j<=10; j++) { The sum of first 10 natural numbers is
sum = sum + j; 55
}
System.out.println("The sum of first 10 na
tural numbers is " + sum);
}
}
Java for-each loop
for(data_type var : array_name/collection_name){
//statements
}
E.g
public class Calculation {
public static void main(String[] args) { Output:
Printing the content of the
// TODO Auto-generated method stub
array names:
String[] names = {"Java","C","C++","Python","JavaScript"}; Java
System.out.println("Printing the content of the array names:\n"); C
for(String name:names) { C++
System.out.println(name); Python
JavaScript
}
}
}
Java while loop

 It is also known as the entry-controlled loop since the condition is checked at the start of the loop.
If the condition is true, then the loop body will be executed; otherwise, the statements after the
loop will be executed.
The syntax of the while loop is given below.
while(condition){
//looping statements
}
public class Calculation {
public static void main(String[] args) { Output:
// TODO Auto-generated method stub Printing the list of first 10
int i = 0; even numbers
System.out.println("Printing the list of fir 0
st 10 even numbers \n"); 2
while(i<=10) { 4
System.out.println(i); 6
i = i + 2; 8
} 10
}
}
Java do-while loop
 The do-while loop checks the condition at the end of the loop after executing the loop statements. When the
number of iteration is not known and we have to execute the loop at least once, we can use do-while loop.It is also
known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop

do
{
//statements
} while (condition);
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
do {
Printing the list of first 10 even
System.out.println(i); numbers
i = i + 2; 0
}while(i<=10); 2
} 4
} 6
8
10
Jump Statements
 Jump statements are used to transfer the control of the program to the specific statements

 Java break statement


The break statement is used to break the current flow of the program and transfer the control to the next
statement outside a loop or switch statement
BreakExample.java
public class BreakExample {
public static void main(String[] args) { Output:
for(int i = 0; i<= 10; i++) { 0
System.out.println(i); 1
2
if(i==6) {
3
break; 4
} 5
} 6
}
}
Java continue statement

The continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next iteration of
the loop immediately.
public class ContinueExample {
public static void main(String[] args) {
// TODO Auto-generated method stub Output:
0
for(int i = 0; i<= 2; i++) {
1
for (int j = i; j<=5; j++) { 2
if(j == 4) { 3
continue; 5
} 1
System.out.println(j); 2
} 3
} 5
}
}
Java - Arrays
 An array is a collection of similar type of elements which has
contiguous memory location.

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.
Types of Array in java
 There are two types of array.
➢ Single Dimensional Array

➢ Multidimensional Array

Single Dimensional Array in Java


Syntax to Declare an Array in Java
dataType[] arr; (or) dataType []arr; (or) dataType arr[];
➢ Instantiation of an Array in Java

arrayRefVar=new datatype[size];
Multidimensional Array in Java
 In such case, data is stored in row and column based index (also known as matrix form).

Syntax to Declare Multidimensional Array in Java


 dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or)

 dataType arrayRefVar[][]; (or) dataType []arrayRefVar[];


//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;
Output:
a[2]=70; 10
a[3]=40; 20
a[4]=50; 70
//traversing array 40
50
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Example of Multidimensional Java Array

//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}}; Output:
//printing 2D array 123
for(int i=0;i<3;i++){ 245
for(int j=0;j<3;j++){ 445
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}

You might also like