0% found this document useful (0 votes)
5 views11 pages

Java Learning

The document provides an overview of Java programming basics, including class structure, the main method, and object creation. It covers fundamental concepts such as strings, inheritance, interfaces, polymorphism, and arrays, along with example code snippets for each topic. The document serves as a guide for beginners to understand and implement basic Java programming principles.

Uploaded by

nirvana nirvana
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)
5 views11 pages

Java Learning

The document provides an overview of Java programming basics, including class structure, the main method, and object creation. It covers fundamental concepts such as strings, inheritance, interfaces, polymorphism, and arrays, along with example code snippets for each topic. The document serves as a guide for beginners to understand and implement basic Java programming principles.

Uploaded by

nirvana nirvana
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/ 11

JAVA

Java Basics :

Java is a general-purpose programming language that is class-based and object-oriented.


The programming language is structured in such a way that developers can write code
anywhere and run it anywhere without worrying about the underlying computer architecture.

In the Java programme, keywords will be highlighted in Pink / Violet color.


Example : Public, class, static, void

Each Java Program has to start with “Class”. Without “Class” there will be no programme to
execute. Class is the file where we write a complete java program.

The execution of the program will be in “Main” method


Ex : public static void main()
{
System.out.println();
}

Main method is the heart of the java programme

If we write the code outside of the Main method, java can’t execute the code.

Java basic Programme :

public class Basic


{

public static void main(String[] args)


{
System.out.println("This is my first program");
}

Output : This is my first program.


Adding two integers programme :

public class Test


{

public static void main()


{
System.out.println("Today i am learning java");

int a=10;
int b=20;
int sum=a+b;
System.out.println("The addition of a and b is" +sum);

Note : Here “int” is the return type and a,b, sum are variables.

Objects In Java :

If we are automating a web application. In that we are having 5 pages and in all the 5 pages
there will be a header section where we need to validate all the links are working or not

In this case, if we write 5 class files for 5 pages and in all the 5 class files, we need to validate
the header section repeatedly in all those class files, which is redundant. To avoid this, write a
method in one class. Write an object class file and call the method.

Take the above addition class.

Now create an other class with method

public class MethodTest


{
public void checking()
{
System.out.println("Calling method has been printed from MethodTest class");
}

}
How to create a object for the class -> classname object = new classname();
Here “new” means we are creating memory allocation to that particular object in the present
class file.

Now call that method in Addition Test class

public class Test


{

public static void main(String[] args)


{
System.out.println("Today i am learning java");

int a=10;
int b=20;
int sum=a+b;
System.out.println("The addition of a and b is" +sum);

MethodTest m = new MethodTest();


m.checking();

Strings in Java :

Strings will be handled when we are having text based content.

For example, we need to validate a text which is “Payment $100 Paid”. Now the amount will be
varied, we are going to validate through $.

STRING is a class, where there will be a lot of methods available in it. STRING class will be
preloaded by JAVA and it will be in the packages.

Previously we learned how to create a object in the class like this

String str1 = new String1(“Payment $100 Paid”);


Str1. -> after dot methos will be called

Now java has been made it very clear like this


String str = ‘Payment $100 Paid’
str.

Code :

public class StringsTest


{

public static void main(String[] args)


{
//String str = "Payment $100 Paid";
String str = new String("Payment $100 Paid");
System.out.println(str.charAt(8));
System.out.println(str.length());
System.out.println(str.indexOf("$"));
System.out.println(str.toUpperCase());
System.out.println(str.substring(8));
}

Output :

$
17
8
PAYMENT $100 PAID
$100 Paid

Palindrome Programme :

Print the String in Reverse or we can say it as Palindrome

public class Panlindrome


{

public static void main(String[] args)


{
String a = "madam";
String b = "";
for(int i=a.length()-1;i>=0;i--)
{
b=b+a.charAt(i);
}

System.out.println(b);

if(b.equalsIgnoreCase(a))

{
System.out.println("String got reversed and string is palindrome");
}
else
{
System.out.println("String got reversed and string is not palindrome");
}

Inheritance :

In Inheritance, we can call one class into another class by using “extends” keyword. And also
we can utilize all the methods from that class.

Example :

public class FatherInherit extends SonInherit {

public static void main(String[] args)

{
FatherInherit fi = new FatherInherit();
fi.Addition();
}

—------------
public class SonInherit {

public static void main(String[] args)


{

public void Addition() {


int a = 10;
int b = 20;
int sum = a + b;
System.out.println("The addition of a and b is" + sum);
}
}

Multiple Inheritance :

public class GrandpaInherit extends FatherInherit


{
public static void main(String[] args)
{
GrandpaInherit gp = new GrandpaInherit();
gp.fathermethod();
gp.Addition();
}
}

Interface :
An interface is an abstract "class" that is used to group related methods with
"empty" bodies: To access the interface methods, the interface must be
"implemented" (kinda like inherited) by another class with the implements keyword
(instead of extends ).

It is a client agreement between methods and class implementation

In Simple words, Interface will have a method, but without body. A class to take initiate to take
an agreement to implement those interface will implement the method which are present in that
particular interface.

Example : Taking Banking client with paycreditcard(), transfer balance() & checkingbalance()
methods
package Test;

public interface BankingClient


{
public void paycreditcard();

public void transferbalance();

public void checkingbalance();


}

Creating class and adding Interface through “implements keyword”

When user implements the interface, we need to add their methods in the present class, if not
we will get this error as “Add unimplemented methods”. Click on “Add unimplemented methods”

Code :
package Test;

package Test;

public class Developingbanking implements BankingClient{

public static void main(String[] args)

{
Developingbanking db = new Developingbanking();
db.checkingbalance();
db.paycreditcard();
db.transferbalance();
db.devlopmethod();
BankingClient bc = new Developingbanking ();
bc.

@Override
public void paycreditcard()
{
System.out.println("paycreditcard");
}

@Override
public void transferbalance()
{
System.out.println("transferbalance");
}

@Override
public void checkingbalance()
{
System.out.println("checkingbalance");
}

public void devlopmethod()


{
System.out.println("Develop method from developing class");
}
}

Here from “Developingbanking db = new Developingbanking();”


We will get all the methods from Developing baking class

We can call two or more number of interface by using comma.

Ex : public class Developingbanking implements BankingClient,ATM

Runtime Polymorphism :
In Java, polymorphism refers to the ability of a class to provide different
implementations of a method, depending on the type of object that is passed to
the method. To put it simply, polymorphism in Java allows us to perform the same
action in many different ways.

From the above code we have used “BankingClient bc = new Developingbanking ();”

In which “BankingClient” is an interface and we are creating object with “bc” and calling all the
method which are present in “BankingClient” interface

In this we are able to see the methods which are related to only “BankingClient” not the
methods from “Developingbanking”

This type of mechanism is call “Run Type Polymorphism”

Arrays :
A container which stores multiple values of the same data type is called an Array.

public class BasicArray


{

public static void main(String[] args)


{
int a[] = new int[5];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
for (int i = 0; i<a.length;i++)

{
System.out.println(a[i]);
}
}

Output :
1
2
3
4
5

Single & Multiple Dimension Arrays :


This is single Dimension Array :

public class BasicArray


{

public static void main(String[] args)


{
int a[] = new int[5];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;

int c[] = {6,7,8,9,10};


for (int i = 0; i<c.length;i++)

{
System.out.println(c[i]);
}

Multi Dimensional Arrays :

In an index, we come across a matrix kind of format. It will be having ‘n’ rows & ‘n’ columns.
Each argument will be taken as X axis and Y axis. X axis is a row and Y axis is a column.
Passing an object in to both X axis and Y axis is nothing but Multi dimensional array. Multi in the
sense we are viewing array from the multi corner perspective like X axis and Y axis.

public class MultiArray


{

public static void main(String[] args)


{
int a[][] = {{1,2,3},{4,5,6}};
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
System.out.println(a[i][j]);
}
}

You might also like