0% found this document useful (0 votes)
4 views44 pages

Lesson 2

The document provides a lesson on primitive types in programming, specifically focusing on 'char', 'float', and 'double'. It includes examples of type casting and reference types like 'String' and 'StringBuffer', along with various methods associated with these types. Additionally, it covers input and output statements, conditional statements, and practical tasks related to string manipulation.

Uploaded by

aiskandarxojayev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views44 pages

Lesson 2

The document provides a lesson on primitive types in programming, specifically focusing on 'char', 'float', and 'double'. It includes examples of type casting and reference types like 'String' and 'StringBuffer', along with various methods associated with these types. Additionally, it covers input and output statements, conditional statements, and practical tasks related to string manipulation.

Uploaded by

aiskandarxojayev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

DastruchilarAkademiyasi gitauzofficial gitauzofficial gita.

uzofficial gitauzofficial
Lesson 2
Primitive Types

DastruchilarAkademiyasi gitauzofficial gitauzofficial gita.uzofficial gitauzofficial


Primitive types - char

char – 16 bit, ishorasiz 0 .. 216 – 1 oraliqdagi qiymatni


qabul qiladi. Unicode kodirovkasidagi belgilar raqamini
saqlanadi.
Primitive types - char

https://unicode-table.com/ru/ - Unicode table


Primitive types - char

char a = ˈaˈ;

char b = ˈAˈ;

char a = ˈ#ˈ;

char a = ˈ~ˈ;
Primitive types - char

char literal = ˈaˈ;


char symbol = ˈ#ˈ;
char tab = ˈ\tˈ;
char linefeed = ˈ\nˈ;
char carriageReturn = ˈ\rˈ;
char apostrophe = ˈ\ˈˈ;
char backslash = ˈ\\ˈ;
char backslash = ˈ\u00A9ˈ; //©
Primitive types – float and double

float a = 1.24;

double b = 1.24;

float a = 1.24f;

double a = 1.24d;
Primitive types – float and double

Type byte sign mantissa exponent

float 32 1 8 23

double 64 1 52 11

𝑒
±𝑚 ∙ 2
Primitive types – float and double

float a = 12.1e-2;

double b = 12.1e+2;// 12.1e2

float hex = 0x1.Fp5;


Primitive types – float and double

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

double positiveInfinity = 1.0 / 0.0;

double negetiveInfinity = -1.0 / 0.0;

double nan = 0.0 / 0.0;


Type casting

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;

a >>> = 1;// a = (byte) (a >>> 1)


Primitive and reference types
Reference types - String

String s = "Hello";

s = s.concat(“world");//qo’shish

s = s.toUpperCase();//katta shiriftga o’tkazish

s = s.toLowerCase();//kichish shiriftga o’tkazish

boolean t = s.contains("ll");
Reference types - String

char c = s.charAt(2);//belgini olish

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

StringBuffer strBuffer = new StringBuffer("Java");


char c = strBuffer.charAt(0); // J
System.out.println(c);
strBuffer.setCharAt(0, 'c');
System.out.println(strBuffer.toString()); // cava
Reference types - String

public static void main(String[] args) {


String numbers = "0123456789";
StringBuilder sb = new StringBuilder(numbers);
System.out.println(sb.substring(3)); //3456789
System.out.println(sb.substring(4, 8)); //4567
System.out.println(sb.replace(3, 5, "ABCDE")); //012ABCDE56789
sb = new StringBuilder(numbers);
System.out.println(sb.reverse()); //9876543210
sb.reverse(); // Вернем изначальный порядок
sb = new StringBuilder(numbers);
System.out.println(sb.delete(5, 9)); //012349
System.out.println(sb.deleteCharAt(1)); //02349
System.out.println(sb.insert(1, "One")); //0One2349
}
Input and Output console

Scanner scanner = new Scanner(System.in);

int a = scanner.nextInt();
Input and Output console

Scanner scanner = new Scanner(System.in);

int a = scanner.nextInt();

int b = scanner.nextInt();

int c = a + b;

System.out.println(c);
Statements – if-else

if(condition) operator1; else operator2;

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

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
if (a >= 0)
System.out.println("Manfiy emas");
else
System.out.println("Manfiy");
System.out.println("Ishladi");

}
Statements – if-else

if(condition) operator1; else operator2;


Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
boolean cond = a > 3;
String s = null;
if(cond) s = "Yes"; else s = "No";
System.out.print(s);
Statements – if-else

if(condition) operator1; else operator2;

Scanner scanner = new Scanner(System.in);

int a = scanner.nextInt();

String s = null;

if(a > 3) s = "Yes"; else s = "No";

System.out.print(s);
Statements – if-else

if(condition) operator1;

Scanner scanner = new Scanner(System.in);

int a = scanner.nextInt();

String s = "No";

if(a > 3) s = "Yes";

System.out.print(s);
Statements – if-else

if(condition){
operator1.1;
operator1.2;
............
} else {
operator2.1;
operator2.2;
...........
}
Statements – if-else

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
if (a >= 0) {
System.out.println("Musbat");
System.out.println("Manfiy emas");
} else {
System.out.println("-");
}

}
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

Ekrandan 2 ta so’z kiritilsin.

Ularni harflarni katta-kichik

hisobga olmagan holda

solishtiring. Natijani mos

ravishda “Bir xil” yoki “Har

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.

Matn ichida so’z bor yoki yo’qligini

aniqlang (harflarni katta-kichik

hisobga olmasin). Natijani mos

ravishda “Topildi” yoki “Topilmadi”

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

Ekrandan matn va 2 ta so’z

kiritilsin. Matn ichidan

birinchi so’zlarni ikkinchi

so’z bilan almashtiring.

Natijani ekranga chiqaring.


almashtirish
Scanner scanner = new Scanner(System.in);
String a = scanner.nextLine();
String b = scanner.next();
String c = scanner.next();
a = a.replace(b, c);
System.out.println(a);
2021

DastruchilarAkademiyasi gitauzofficial gitauzofficial gita.uzofficial gitauzofficial

You might also like