Lesson 2
Lesson 2
uzofficial gitauzofficial
Lesson 2
Primitive Types
char a = ˈaˈ;
char b = ˈAˈ;
char a = ˈ#ˈ;
char a = ˈ~ˈ;
Primitive types - char
float a = 1.24;
double b = 1.24;
float a = 1.24f;
double a = 1.24d;
Primitive types – float and double
float 32 1 8 23
double 64 1 52 11
𝑒
±𝑚 ∙ 2
Primitive types – float and double
float a = 12.1e-2;
double sum = a + b;
double diff = a - b;
double mult = a * b;
double div = a / b;
double rem = a % b;
double inc = a++ + ++b;
double dec = --a – b--;
Primitive types – float and double
byte a = 123;
short b = a;
int c = b;
long d = c;
Type casting
char a = ˈaˈ;
int b = a;
long c = a;
Type casting
int a = 123;
float b = a;
double c = a;
double e = b;
Type casting
int a = 1024;
byte b = (byte) a; // 0
double pi = 3.14;
int a = (int) pi; // 3
float pi = 3.14;
int a = (int) pi; // 3
double a = 10e100;
float b = (float) a;
Type casting
double a = 7d + 7f;
float a = 17f – 1;
long c = 1L – ˈaˈ;
byte a = 12;
byte b = 15;
byte c = (byte) (a + b);
Type casting
byte a = 1;
a += 7;// a = (byte) (a + 7)
byte a = 1;
String s = "Hello";
s = s.concat(“world");//qo’shish
boolean t = s.contains("ll");
Reference types - String
int a = s.length();//uzunlik
s = s.replace("l","L");//almashtirish
s = s.trim();
boolean b = s.isEmpty();
Reference types - String
Method Description Return Type
charAt() Returns the character at the specified index (position) char
codePointAt() Returns the Unicode of the character at the specified index int
codePointBefore() Returns the Unicode of the character before the specified index int
codePointCount() Returns the Unicode in the specified text range of this String int
compareTo() Compares two strings lexicographically int
compareToIgnoreCase() Compares two strings lexicographically, ignoring case differences int
concat() Appends a string to the end of another string String
contains() Checks whether a string contains a sequence of characters boolean
contentEquals() Checks whether a string contains the exact same sequence of characters of the specified CharSequence or boolean
StringBuffer
copyValueOf() Returns a String that represents the characters of the character array String
endsWith() Checks whether a string ends with the specified character(s) boolean
equals() Compares two strings. Returns true if the strings are equal, and false if not boolean
equalsIgnoreCase() Compares two strings, ignoring case considerations boolean
format() Returns a formatted string using the specified locale, format string, and arguments String
getBytes() Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array byte[]
getChars() Copies characters from a string to an array of chars void
hashCode() Returns the hash code of a string int
indexOf() Returns the position of the first found occurrence of specified characters in a string int
intern() Returns the canonical representation for the string object String
isEmpty() Checks whether a string is empty or not boolean
lastIndexOf() Returns the position of the last found occurrence of specified characters in a string int
Reference types - String
Method Description Return Type
length() Returns the length of a specified string int
matches() Searches a string for a match against a regular expression, and returns the matches boolean
offsetByCodePoints() Returns the index within this String that is offset from the given index by codePointOffset code int
points
regionMatches() Tests if two string regions are equal boolean
replace() Searches a string for a specified value, and returns a new string where the specified values are String
replaced
replaceFirst() Replaces the first occurrence of a substring that matches the given regular expression with the given String
replacement
replaceAll() Replaces each substring of this string that matches the given regular expression with the given String
replacement
split() Splits a string into an array of substrings String[]
startsWith() Checks whether a string starts with specified characters boolean
subSequence() Returns a new character sequence that is a subsequence of this sequence CharSequence
substring() Returns a new string which is the substring of a specified string String
toCharArray() Converts this string to a new character array char[]
toLowerCase() Converts a string to lower case letters String
toString() Returns the value of a String object String
toUpperCase() Converts a string to upper case letters String
trim() Removes whitespace from both ends of a string String
valueOf() Returns the string representation of the specified value String
Reference types - String
Reference types - String
Reference types - StringBuffer
int a = scanner.nextInt();
Input and Output console
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = a + b;
System.out.println(c);
Statements – if-else
int a = 3;
boolean cond = a > 3;//false
String s = null;
if(cond) s = "Yes"; else s = "No";
System.out.print(s);
Statements – if-else
}
Statements – if-else
int a = scanner.nextInt();
String s = null;
System.out.print(s);
Statements – if-else
if(condition) operator1;
int a = scanner.nextInt();
String s = "No";
System.out.print(s);
Statements – if-else
if(condition){
operator1.1;
operator1.2;
............
} else {
operator2.1;
operator2.2;
...........
}
Statements – if-else
}
Task - Ism
Ismingizni kiriting:
Alisa
Sizinig ismingiz: Alisa
Task - Ism
System.out.println("Ismingizni kiriting:");
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
System.out.print("Sizinig ismingiz: ");
System.out.print(s);
Bir xillikka tekshirish
xil” chiqaring.
Bir xillikka tekshirish
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
String b = scanner.next();
if (a.equalsIgnoreCase(b)){
System.out.println("Bir xil");
} else {
System.out.println("Har xil");
}
Matn ichidan so’z izlash
Ekrandan matn va so’z kiritilsin.
chiqaring.
Matn ichidan so’z izlash
Scanner scanner = new Scanner(System.in);
String a = scanner.nextLine();
String b = scanner.next();
a = a.toLowerCase();
b = b.toLowerCase();
if (a.contains(b)){
System.out.println("Topildi");
} else {
System.out.println("Topilmadi");
}
almashtirish