Created
September 8, 2021 16:22
-
-
Save MananAmin/6461104a9b87d096e917e9ead4bda9f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.learn; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println("Hello World"); | |
// variables | |
String name = "Manan"; | |
// primitive type | |
int number = 007; | |
double pi = 3.14; | |
long phone = 7359251716L; | |
byte temp = 127; | |
float pi2 = 3.142F; | |
char c = 'a'; | |
boolean flag = true; | |
// new keyword | |
String str = new String("Hello guys"); | |
//Strings | |
String str1 = "lost"; | |
String str2 = "boy"; | |
String con_str = str1 + str2; | |
System.out.println(con_str.charAt(4)); | |
String str3 = str1.replace('a','b'); | |
String sub = str1.substring(0,4); | |
System.out.println(sub); | |
// Arrays | |
int len = 100; | |
int[] array = new int[len]; | |
boolean[] bool_array = new boolean[len]; | |
int length = array.length; | |
//sort array | |
Arrays.sort(array); | |
int[][] matrix= new int[len][len]; | |
// casting | |
double price = 100.50; | |
double final_price = price - 10; // Example of implicit casting | |
int cost = 100; | |
int final_cost = cost - (int)18.9; // Explicit casting | |
// Constants | |
final float PI = 3.14F; | |
//Operators | |
int val1 = 100; | |
int val2 = 50; | |
int sum = val1 + val2 - (int)PI; | |
int mod = val1 % val2; | |
boolean antiflag = !flag; | |
sum++; | |
--sum; | |
// Math class | |
Math.max(val1,val2); | |
double ran = 100*Math.random(); | |
//input | |
Scanner sc = new Scanner(System.in); | |
// int num = sc.nextInt(); | |
// String name2 = sc.next(); // input until space | |
// String line = sc.nextLine(); // Inute line | |
// comparison operators | |
boolean is_eq = val1 ==val2; | |
// Condition statements | |
if(val1 > val2){ | |
System.out.println("Val1 is large"); | |
} | |
else { | |
System.out.println("Val2 is large"); | |
} | |
// logical operators | |
if(name=="Manan" &&val1 > 18){ | |
System.out.println("okay "); | |
} | |
// switch | |
int day = 1; | |
switch(day){ | |
case 1 : | |
System.out.println("Monday"); | |
case 2: | |
System.out.println("Tuesday"); | |
break; | |
default: | |
System.out.println("Holiday"); | |
} | |
// loops | |
for(int i =0 ; i < array.length ;i++){ | |
System.out.println(i); | |
if(i==37){ | |
continue; | |
} | |
} | |
int fl = 30; | |
while(fl>0){ | |
System.out.println(fl); | |
fl--; | |
} | |
int k =1; | |
do{ | |
System.out.println("Enter Even number"); | |
k = sc.nextInt(); | |
}while(!isEven(k)); | |
// exception handling | |
try{ | |
System.out.println(array[1000]); | |
}catch(Exception ex){ | |
System.out.println("Inside Exception "+ ex); | |
} | |
} | |
// function | |
public static boolean isEven(int number){ | |
if(number % 2==1) | |
return false; | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment