SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class

-> static block:

package org.example.test3;

public class Blocks {
static {
System.out.println("In static block");
}

public static void method() {
System.out.println("In static method");
}
}


package org.example.test3;

public class Main {
public static void main(String[] args) {
Blocks.method();
}
}


-> Class.forName() metodunu ishe salmaq:

package org.example.test3;

public class Blocks {
static {
System.out.println("In static block");
}
}
package org.example.test3;

public class Main {
public static void main(String[] args) throws Exception{
Class.forName("org.example.test3.Blocks");
}
}


-> non-static (instance) block:

package org.example.test3;

public class Blocks {
static {
System.out.println("in static block");
}

{
System.out.println("in non static (instance) block");
}

public Blocks() {
System.out.println("in constructor");
}
}
package org.example.test3;

public class Main {
public static void main(String[] args) throws Exception{
Blocks blocks = new Blocks();
}
}


-> Problem 1:

package org.example.test3;

public class Blocks {
public static int count;

public Blocks() {
count = 5;
}
}
package org.example.test3;

public class Main {
public static void main(String[] args) {
System.out.println(Blocks.count);
}
}


-> Solution 1:

package org.example.test3;

public class Blocks {
public static int count;

static {
count = 5;
}

public Blocks() {
count = 5;
}
}
package org.example.test3;

public class Main {
public static void main(String[] args) {
System.out.println(Blocks.count);
}
}


-> Problem2:

package org.example.test3;

public class Blocks {
static {
System.out.println("in static block");
}

{
System.out.println("in non static (instance) block");
System.out.println("do something");
}

public Blocks() {
System.out.println("in constructor");
System.out.println("do something");
}

public Blocks(int a) {
System.out.println("in constructor with parameter list");
System.out.println("do something");
}
}


-> Solution:

package org.example.test3;

public class Blocks {
static {
System.out.println("in static block");
}

{
System.out.println("in non static (instance) block");
System.out.println("do something");
}

public Blocks() {
System.out.println("in constructor");
}

public Blocks(int a) {
System.out.println("in constructor with parameter list");
}
}


-> Problem3: Ferz edekki burada DB-dan xususi melumat getirir.

package org.example.test3;

public class Blocks {
public static int count;

public static void foo() {
count = 5;
}
}
package org.example.test3;

public class Main {
public static void main(String[] args) throws Exception{
System.out.println(Blocks.count);
}
}


-> Solution:

package org.example.test3;

public class Blocks {
public static int count;

static {
System.out.println("Get some info from DB");
count = 5;
System.out.println("Assign this info to variable");
}
}



-> Boxing, autoboxing, unboxing, autounboxing

int i = 5; // Primitive datatype
Integer ii = new Integer(i); // Boxing - Wrapping
int j = ii.intValue(); // Unboxing - Unwrapping
Integer value = i; // Autoboxing - Autowrapping
int k = value; // Autounboxing - Autounwrapping


-> Primitiv ve referans integer-leri (==) muqayise etmek

Integer i1 = 5;
Integer i2 = 5;
Integer i3 = new Integer(5);
Integer i4 = new Integer(5);

System.out.println(5 == i1);
System.out.println(i1 == i2);
System.out.println(i1 == i3);
System.out.println(i3 == i4);
System.out.println(i3 == 5);


-> Integer pool anlayishi ve referans integer-leri (==) muqayise etmek

Integer i1 = 5;
Integer i2 = 5;
Integer i3 = 400;
Integer i4 = 400;
Integer i5 = new Integer(5);
Integer i6 = new Integer(5);
Integer i7 = new Integer(400);

System.out.println(i1 == i2);
System.out.println(i3 == i4);
System.out.println(i3 == i7);
System.out.println(i1 == i5);
System.out.println(i5 == i6);
System.out.println(i1 == 5);
System.out.println(i3 == 400);
System.out.println(i5 == 5);
System.out.println(i7 == 400);




-- Math round, floor, ceil

package org.example;

public class Main2 {
public static void main(String[] args) {
System.out.println(Math.round(2.4));
System.out.println(Math.floor(2.9));
System.out.println(Math.ceil(1.1));
}
}

-- Math pow, sqrt

package org.example;

public class Main2 {
public static void main(String[] args) {
System.out.println(Math.pow(5, 3));
System.out.println(Math.sqrt(25));
}
}

-- Math random

package org.example;

public class Main2 {
public static void main(String[] args) {
System.out.println((int) (Math.random() * 10));
System.out.println((int) (Math.random() * 10));
System.out.println((int) (Math.random() * 10));
System.out.println((int) (Math.random() * 10));
}
}


-- Random class

Random random = new Random();
int start = 10;
int end = 20;
int i = random.nextInt(end - start) + start;
System.out.println(i);



String class:

1. equals problem:

package org.example7;

public class Main {
public static void main(String[] args) {
String s = null;
System.out.println(s.equals("java"));
System.out.println("java".equals(s));
}
}


-> Combining many trings with + and concat methd, and difference between them

*** 

package org.example.lesson9;

public class Main {
public static void main(String[] args) throws Exception {
String s = "Parvin";
System.out.println(s.hashCode());
s += "Nermin";
System.out.println(s.hashCode());

String ss = "java";
System.out.println(ss.hashCode());
ss = ss.concat("dersleri");
System.out.println(ss.hashCode());

StringBuilder sb = new StringBuilder("Parvin");
System.out.println(sb.hashCode());
sb.append("Nermin");
System.out.println(sb.hashCode());
}
}

*** 



-- Performance rate

package org.example4;

public class Main {
public static void main(String[] args) {
buildStringWithPlus("Java", 100_000);
System.out.println("-----------------------------------");
buildStringWithStringBuilder("Java", 100_000);
System.out.println("------------------------------------");
buildStringWithStringBuffer("Java", 100_000);
}

public static void buildStringWithPlus(String string, int count) {
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
string = string + i;
}
long end = System.currentTimeMillis();
System.out.println("Time elapsed: " + (end - start));
System.out.println("String length: " + string.length());
}

public static void buildStringWithStringBuilder(String string, int count) {
StringBuilder sb = new StringBuilder(string);
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
sb.append(i);
}
long end = System.currentTimeMillis();
System.out.println("Time elapsed: " + (end - start));
System.out.println("String length: " + sb.toString().length());
}

public static void buildStringWithStringBuffer(String string, int count) {
StringBuffer sf = new StringBuffer(string);
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
sf.append(i);
}
long end = System.currentTimeMillis();
System.out.println("Time elapsed: " + (end - start));
System.out.println("String length: " + sf.toString().length());
}
}


-- LocalTime

LocalTime localTime = LocalTime.now();
System.out.println("Hour: " + localTime.getHour());
System.out.println("Minute: " + localTime.getMinute());
System.out.println("Second: " + localTime.getSecond());
System.out.println("Nano second: " + localTime.getNano());










-> Sozun palindromlugunu yoxlamaq:

public static boolean palindrome(String str) {
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i))
return false;
}
return true;
}

 

-> trim() metodunun tetbiqi:

String str = " salam ";
String one = str.trim();
System.out.println(str);
System.out.println(one);


-> replace() metodunun tetbiqi:

String str = " salam ";
String one = str.replace(" ", "");
System.out.println(str);
System.out.println(one);


-> isEmpty() ve isBlank()

String one = "";
System.out.println(one.isEmpty());
System.out.println(one.isBlank());

String two = " ";
System.out.println(two.isEmpty());
System.out.println(two.isBlank());

String three = "something";
System.out.println(three.isEmpty());
System.out.println(three.isBlank());


-> Verilmish cumledeki butun boshluqlari aradan qaldirmaq:

 String str = " hello   how are      you?      ";
System.out.println(str);
String[] s = str.split(" ");
String result = "";
for (int i = 0; i < s.length; i++) {
String element = s[i];
System.out.println(element);
if (element != "") {
if (i != s.length - 1)
result += element + " ";
else result += element;
}
// if(!element.isEmpty()) {
// result += element + " ";
// }
// if(!element.isBlank()) {
// result += element + " ";
// }
// if(element != "") {
// result += element + " ";
// }
}

System.out.println(result);









Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism