Вы просматриваете старую версию данной страницы. Смотрите текущую версию.

Сравнить с текущим просмотр истории страницы

« Предыдущий Версия 12 Следующий »

Структура JVM

Структура памяти

В документации полностью описаны области памяти в JVM - https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html

Сборщик мусора

Документация по GC - https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html


Строки

Интернирование

Пример интернирования
String s1 = "Hello, World";
String s2 = "test";
System.out.println("Comparing s1 and s2");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

System.out.println("Comparing s1 and s3");
String s3 = new String(s1);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));

System.out.println("Interning s1 and s3");
System.out.println(s1 == s3.intern());

System.out.println("Comparing concatenated strings");
String s4 = s1+"!";
System.out.println(s4 == s1+"!");
System.out.println(s4 == (s1+"!").intern());
System.out.println(s4.intern() == (s1+"!").intern());
System.out.println(s4.equals(s1+"!"));

Полезные ссылки

    1. String pooling - http://java-performance.info/string-intern-in-java-6-7-8/


Методы

Примеры работы со строками
String description = "The class String includes methods for examining\n" +
        " * individual characters of the sequence, for comparing strings, for\n" +
        " * searching strings, for extracting substrings, and for creating a\n" +
        " * copy of a string with all characters translated to uppercase or to\n" +
        " * lowercase";

System.out.println("Length: "+description.length()+", is empty: "+description.isEmpty());

String[] words = description.split(" ");
for(String word : words) {
    System.out.println(word);
}

String shortDescription =  description.substring(10);
System.out.println(shortDescription);
shortDescription =  description.substring(description.indexOf("String"));
System.out.println(shortDescription);

String usdNumber = "$101.78";
System.out.println(usdNumber.matches("\\$\\d*\\.\\d*"));

String stringWithPrice = "Cost is 101.56 USD in Target";
System.out.println(stringWithPrice.replaceAll("(\\d*\\.\\d*)( USD)", "\\$$1"));

String[] numbers = new String[] {"one", "two", "three"};
System.out.println(String.join(",", numbers));


StringBuilder  и StringBuffer

Пример использования StringBuilder
//плохо
String result = "0";
for (int i = 1; i < 10000; i++) {
    result += "," + i;
}
System.out.println(result);


//хорошо
StringBuilder sb = new StringBuilder("0");
for (int i = 0; i < 10000; i++) {
    sb.append(",").append(i);
}
System.out.println(sb.toString());


Числа


Задачи

CurrencyExchenge

Необходимо реализовать класс Money с поддержкой валют и методами add, subtract, multiply, devide

Постановка: CurrencyExchange.zip

Quantity


Материалы

Презентация: Типы данных в Java


  • Нет меток