Java Basics Batch30
Java Basics Batch30
System.out.println(d);
[ ] System.out.println("Byte min:"+Byte.MIN_VALUE);
System.out.println("Byte max:"+Byte.MAX_VALUE);
System.out.println("Short min:"+Short.MIN_VALUE);
System.out.println("Short max:"+Short.MAX_VALUE);
System.out.println("Int max:"+Integer.MIN_VALUE);
System.out.println("Int max:"+Integer.MAX_VALUE);
System.out.println("Long mix:"+Long.MIN_VALUE);
System.out.println("Long max:"+Long.MAX_VALUE);
[ ] int j=5;
int k=++j+j++; // j+=1;k = j+j; j=j+1
System.out.println(j+" "+k);
//byte b = 127;
//System.out.println(++b);
//32 >>2;
[ ] public class {
public static void main () {
}
}
[ ] double a = 3.14159;
System.out.println(a);
System.out.println(a + 1);
System.out.println(8 / (int) a);
System.out.println(8 / a);
System.out.println((int) (8 / a));
double x = 4;
int x = 3.5;
[ ] int income=50000;
double rate;
if (income < 47500) rate = 0.22;
else {if (income < 114650) rate = .25;
else {if (income < 174700) rate = 0.28;
else {if (income <311950) rate = 0.33;
else rate = 0.35;
}
}
}
System.out.println(rate);
System.out.println(rate);
[ ] int dow=2;
if (dow==0)System.out.println("Sunday");
else if (dow==1)System.out.println("Monday");
else if (dow==2)System.out.println("Tuesday");
else System.out.println("Thursday");
[ ] //trenary operator
int a=12;int b=8;int min=0; boolean bb=true;
if (a<b) min=a;
else min=b;
System.out.println(min);
[ ] min = (a<b) ? a : b;
System.out.println(min);
[ ] int income=50000;
double rate = 0.35;
if (income < 311950) rate = 0.33;
if (income < 174700) rate = 0.28;
if (income < 114650) rate = 0.25;
if (income < 47450) rate = 0.22;
System.out.println(rate);
For loop
for(initialize;loop continuation; loop condition update) {
statement 1;
statement 2;
statement 3;
.....
}
1. initialize
2. check loop continuation condition,
3. if true, do
3.1 statement 1;
3.2 statement 2;
3.3 statement 3;
4 loop condition update
5. Go back to 2
[ ] int i=1;
for (;;) {
if (i>8){ break;} // i<=8
System.out.print(i+" ");
i++;
}
[ ] int f = 0, g = 1;
for (int i = 0; i <= 5; i++)
{ System.out.print(f+" ");
// i = 0 1 2 3 4 5
f = f + g; // f = 0 1 1 2 3 5 8
g = f - g; // g = 1 0 1 1 2 3 5
}
// 0 1 1 2 3 5 8 //fibonnaci
2147483547
102
// 0 6 1 7 2 8 3 8
0 6 1 7 2 8 3 9 4 9
1 2 3 4 5 6 7 8
[ ] do { 1. add
2. sub
3. multiply
4. divide
5. exit
System.out.println(prime);
System.out.println(sum);
35
I. *
**
***
****
*****
II.
*****
****
***
**
*
System.out.println();
}
*****
****
***
**
*
*...*
.*.*.
..*..
.*.*.
*...*
*...*
.*.*.
..*..
.*.*.
*...*
System.out.println();
}
*...*
.*.*.
..*..
.*.*.
*...*
Strings
String is a sequence of Unicode characters
• The String class belongs to the java.lang package, which does not require an import
statement
• A string literal is a sequence of characters between double quotes
doesn’t have to be constructed
can be passed to methods and constructors as parameters
• There is no primitive type for representing strings in Java
• In Java, ordinary strings are objects of the class String
• String that has zero character is called an empty String “”
• Unlike other classes, String has two operators, + and += (usedfor concatenation)
false
Special Characters
• Strings can include special characters by providing escape sequences
• \u / a Unicode character /
• \b / \u0008: backspace BS /
• \t / \u0009: horizontal tab HT /
• \n / \u000a: linefeed LF /
• \f / \u000c: form feed FF /
• \r / \u000d: carriage return CR /
• \" / \u0022: double quote " /
• \' / \u0027: single quote ' /
• \ / \u005c: backslash \ /
//s.indexOf("z",0);
s.lastIndexOf("b",6);
//s1.equalsIgnoreCase(s3);
15
c
s.contains("day");
String s1 ="GaneshMo";
String s3= "Ganeshmo";
s1.compareTo(s3); // + s1 > s3 -ve s1 < s3 0 s1 = s3
s1.equalsIgnoreCase(s3);
String [] st = s.split("y");
System.out.println(Arrays.toString(st));
[Toda, is Thursda]
System.out.println(name.equals("ganesh"));
System.out.println("ganesh".equals(name));
false
false
StringBuilder
Constructors -
StringBuilder() - empty stringbuilder builds with a capacity of
16
StringBuilder(int capacity) - builds with a given capacity
StringBuilder(String S) - builds with a capacity of length of
S + 16
l=23
39
40 80
This is a StringBuilderHi How are you??#
methods of StringBuilder
[ ] apart from length(), charAt(), subString() stringBuilder has its own meth
setLength() - sets the length of the string, this may result in truncatio
reverse() - the characters in the string are replaced with the characte
setCharAt (int index, char ch) - replaces the character at index postio
delete (int start, int end) - will delete string of characters from s
true
true
StringTokenizer
Constructors :
10
Today
is
wednesday.
7
methods of StringTokenizer
while (st.hasMoreTokens()) {
String s = st.nextToken();
String delim = st.nextToken();
StringTokenizer st1 = new StringTokenizer(s);
if (delim.equals(".")) while (st1.hasMoreTokens()) {System.out.pr
if (delim.equals("?")) while (st1.hasMoreTokens()) {System.out.pr
if ("!".equals(delim)) while (st1.hasMoreTokens()) {System.out.pr
// if (!delim.equals("!")) while (st1.hasMoreTokens()) {System.out
System.out.println();
}
Today,is,working,day,
It#is#not#a#holiday#
Its@a@nice@weather@
Today
i=0 6
is
i=1 5
working
i=2 4
day
i=3 3
not
i=4 2
a
i=5 1
holiday
i=6 0
123
[24] // find the sum of the numbers from the text "abcde789efg9765uyxt123"
String s= "34abcde789efg9765uyxt123"; // 789+9765+123 =
/*String n = "0123456789"; String num=""; int sum=0;
for (int i=0; i<s.length();i++) {
char ch = s.charAt(i);
if (n.contains(ch+"")) {num+=ch;}
else { if (num !="")
{sum += Integer.parseInt(num); num="";}
}
}
if (num !="")
{sum += Integer.parseInt(num); num="";} */
System.out.println(sum);
--------------------------------------------------------------------------
-
java.lang.NumberFormatException: For input string: "t123"
at
java.base/java.lang.NumberFormatException.forInputString(NumberFormatExcep
tion.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at .(#72:1)
[ ]
[ ]