Java:
Structure:
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Variables and types:
Data types:
Operators and operands:
control statements:
if if else for while do while
Input in java:
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("Name is :"+name);
}
}
Other data types:
sc.nextInt();
sc.nextFloat();
sc.nextLong();
sc.nextBoolean();
sc.next().charAt();
import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//sc.nextLine();
String name = sc.nextLine();
System.out.println("Name is :"+name);
System.out.println("Name is :"+n);
}
}
Arrays in java:
int arr[] = {1,2,3};
int myarr[] = new int[5];
for each loop:
for(int i: arr){
System.out.println(i);
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
int arr[]= new int[3];
arr[0]=1;
arr[1]=5;
arr[2]=2;
for(int i:arr)
System.out.println(i);
}
}
Functions :
import java.util.*;
public class Main
{
static int sumofnums(int a,int b){
return a+b;
}
public static void main(String[] args) {
System.out.println(sumofnums(1,2));
}
}
0 Comments