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

Java

The document provides a comprehensive guide for setting up a Java development environment, including installing JDK and Eclipse IDE. It details the creation of a Java project, package, and class, along with coding examples for basic Java constructs such as variables, data types, loops, and arrays. Additionally, it covers rules for naming classes and includes sample programs for printing text, calculating sums, and identifying prime numbers.

Uploaded by

udhaiabi7
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)
0 views

Java

The document provides a comprehensive guide for setting up a Java development environment, including installing JDK and Eclipse IDE. It details the creation of a Java project, package, and class, along with coding examples for basic Java constructs such as variables, data types, loops, and arrays. Additionally, it covers rules for naming classes and includes sample programs for printing text, calculating sums, and identifying prime numbers.

Uploaded by

udhaiabi7
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/ 7

Environmental setup.

1) JDK/java===jdk11
2) IDE(eclipse, intellijide etc)

1. Install JDK.
a) Signup and download jdk. https://www.oracle.com/in/java/technologies/downloads/
Once install JDK then we have to set Java path.
A) This PC->C:\Program Files\Java\jdk-24\bin (copy this path) and.
B) Search environment variable->select “Edit the system environment variable”->click on
“environment variable”->Under “System Variable->select “path” and click “Edit”->New->paste the
path “C:\Program Files\Java\jdk-24\bin” and click “Ok” for all.

b) For test, open terminal/command promt, type Java -version, it will list the version details.

2. Install Eclipse IDE.


A) https://www.eclipse.org/downloads/packages/
B) Then double click on Eclipse and select “Eclipse ide for java developers.”
C) Click on launch and give path for store the project “C:\Workspaces\Javasel”.
===================================================================================
1. Create a new java project in eclipse.
a) Click on “Create a Java project”.
b) give a name “JavaProgramming”. don’t give space
c) Click on “Finish”.
2. Create a new java package.
d) JavaProgramming->src->java Package->name “day1”->finish.
3. Create a new class for create a java program.
a) JavaProgramming->src->java Package->name “day1”->new class->name
“Firstjavaprogram”. and don’t give space->Finish.
i. Class name always should be start with uppercase.
package day1;

public class FirstJavaProgram {

Main Method:
1. Highlighted one is main method.
2. If you want to execute the class, main method must be there. We cant run the program
without main method.
package day1;

public class FirstJavaProgram


{
public static void main (String arg[])
{
}}

Program for print the text:

package day1;

public class FirstJavaProgram


{
public static void main (String args[])
{
System.out.println("Welcome to java");
System.out.println(10+20);
}
}

Output:

Rules:
1) Class name should start with uppercase.
2) Class name should not start with number.
3) Class name contain _(UnderScore) but not hypen (-)
4) Special characters are not allowed.
5) Class name contains number/s.

Comments:
1) Single line comments //---java will not execute.
2) Multi line comments. Paragrah type comments /*paragraph*/

Variables & Data Types

Variable
A variable is a container we can store some data.
Eg: x=200;
We cant able to store varible like above. We have to mention data
type.

Eg: int x=200; float v=10.75; string name=”udhay”.

Int,string,float is some example data type and x,v,name is a


variable.

Int a; //declaration.
a=100; //assignment.

Variables are not constant.

DataTypes:
1) Primitive datatypes - to store single data at a time.
a) Eg: int a=100; a=200;
b) byte,short,int,long ---- Number..
c) Fload,double ---decimal number
d) Char -single character ‘a’ (single quot)
e) Boolean - (True/False).
2) Non-Primitive datatype.
a) String,
b) ArryaList (int a[]=new int[100] we can store 100 variables,
c) HashMap,
d) HashSet etc…
While Loop:

package day5;

public class WhileLoopDemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
//Example: 1 print 1 to 10 numbers.
/*int i=1; //initialization.
while(i<=10) //condition
{
System.out.println(i);
i++; //inc
}*/
//Example: 2 print hello 10times.
/*int i=1; //initialization.
while(i<=10) //condition
{
System.out.println("Hello");
i++; //inc
}*/
//Example 3: print even numbers from 1 to 10
/*int i=2;
while(i<=10)
{
System.out.println(i);
i+=2;
}*/
//Example 4: print each number is odd or even with that number.
/*int i=1;
while(i<=10)
{
if(i%2==0) {
System.out.println(i + " Even Number");
}
else
{
System.out.println(i + " Odd Number");
}
i++;
}*/
//Example 5:print number from 1 to 10 in decreasing order.
int i=10;
while(i>=1) //i>0
{
System.out.println(i);
i--;
}
}

Dimentional array:

package day6;

public class TwoOrMultiDimentional {

public static void main(String[] args) {


// TODO Auto-generated method stub
//approach 1
/*int a[][]=new int[3][2];
a[0][0]=100;
a[0][1]=200;
a[1][0]=300;
a[1][1]=400;
a[2][0]=500;
a[2][1]=600;*/
//approach 2
int a[][]= {{100,200},
{300,400},
{500,600}};
//System.out.println("Lenght of rows" + a.length);
//System.out.println("LENGHT OF ROWS" + a[0].length);
//read single value from array
//System.out.println(a[2][1]);
// Normal for loop
/*for(int r=0;r<a.length;r++)
{
for(int c=0;c<=1;c++)
{
System.out.println(a[r][c]);
}
System.out.println();
}*/
//enhanced for loop method
for (int arr[]:a)
{
for (int x:arr)
{
System.out.println(x+ " ");
}
System.out.println();
}
}

Object Variable:

Eg: Object a[]={100,10.5,’A’,”Welcome”,true}

In object variable we can store any type of data like string, float,
double,boolean,char…
package day6;

public class ObjectArray {

public static void main(String[] args) {


Object a[]= {100,10.5,'A',"Welcome",true};
/*for(Object x:a)
{
System.out.println(x);
}*/
for(int i=0;i<=a.length-1;i++)
{
System.out.println(a[i]);
}

Find the sum of Element of an array


a[]={1,2,3,4,5} O/P=15 (1+2+3+4+5)

package day6;

public class SumOfElementOfArray {


public static void main(String[] args) {
int a[]= {1,2,3,4,5};
int sum=0;
for(int i=0;i<=a.length-1;i++)
{
sum += a[i];
}
System.out.println(sum);
}

Print Even and Odd numbers arrays and count of that array

package day6;

public class EvenOddArray {

public static void main(String[] args) {


int[] myArray = {22, 10, 5, 9, 6};
int evencount=0;
int oddcount=0;
System.out.println("Even numbers in the given array are:: ");

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


if(myArray[i]%2 == 0){
evencount++;
System.out.println(myArray[i]);
}
}
System.out.println("Odd numbers in the given array are:: ");

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


if(myArray[i]%2 != 0){
oddcount++;
System.out.println(myArray[i]);
}
}
System.out.println("Even number count is" + evencount);
System.out.println("Odd number count is" + oddcount);
}

Prime Number or Not:

package day6;

public class PrimeNumberorNot {

public static void main(String[] args) {


// TODO Auto-generated method stub
int a=23;
boolean isprime=true;
for(int i=2; i<a; i++)
{
if(a%i == 0)
{
isprime=false;
}
}
if(isprime)
{
System.out.println("This is a prime number");
}
else
{
System.out.println("Not a Prime number");
}
}

You might also like