Skip to content
Etsu edited this page Aug 25, 2017 · 28 revisions
                                         ____.                    
                                        |    |____ ___  _______   
                                        |    \__  \\  \/ /\__  \  
                                    /\__|    |/ __ \\   /  / __ \_
                                    \________(____  /\_/  (____  /
                                                  \/           \/ 

Java

Install Java

sudo add-apt-repository ppa:webupd8team/java
aptitude update
aptitude search oracle
aptitude install oracle-java8-installer oracle-java7-installer

Compilation

javac HelloWorld.java
javap -v HelloWorld.class 
java HelloWorld
java -classpath classes_dir HelloWorld

Компиляция со сторонними классами (библиотеками)

java -classpath lib.jar HelloWorld.java
java -classpath lib.jar:hw.jar HelloWorld

JAR

Упаковка

jar cfe hw.jar HelloWorld HelloWorld.class

Просмотреть содержимое архива без распаковки

jar tf hw.jar

Распаковка архива

jar xf hw.jar

Запуск

Запуск программы, если прописан главный класс

java -jar hw.jar

Else:

java -classpath hw.jar HelloWorld

Codestyle

Более подробно о рекомендуемом стиле кода можно почитать здесь:

Assembly:

Ant - build.xml

Gradle - build.gradle

Maven - pom.xml

Print type

System.out.println(variable.getClass().getName());

Классы-обёртки (wrapper classes)

int primitive = 0
// boxing
Integer reference = Integer.valueOf(primitive);
// unboxing
int backToPrimitive = reference.intValue();

Конвертация в строку и обратно

long fromString = Long parseLong("12345");
String fromLong = Long.toString(12345);
String concatenation = "area" + 51;

Simple way to print a Java array

String[] array = new String[] {"John", "Mary", "Bob"};
System.out.println(Arrays.toString(array));

Arrays

Varargs

static int maxArray(int[] numbers) { ... };
maxArray(new int[] {1, 2, 3});

static int maxVarargs(int... numbers) { ... };
maxVarargs(1, 2, 3);

Array equals

import java.util.Arrays;

int[] a = {1, 2, 3};
int[] b = {4, 5, 6};
boolean equals = Arrays.equals(a, b);
boolean equals = Arrays.deepEquals(a, b);

Print Array

int[] a = {1, 2, 3};
System.out.println(Arrays.toString(a));
System.out.println(Arrays.deepToString(a));

Strings

char[] charArray = {'a', 'b', 'c'};
String string = new String(charArray);
char[] charFromString = string.toCharArray();

String s = "stringIsImmutable";
int length = s.length();
char firstChar = s.charAt(0);

boolean endsWithTable = s.endsWith("table");
booalen containsIs = s.contains("Is");
String substr = s.substring(0, 6);

String afterReplace = s.replace("Imm", "M");
String allCapitals = s.toUpperCase();
boolean equal2 = s1.equalsIgnoreCase(s2);

Ternary operator

if (weatherIsGood) {
    System.out.println("Wheater is good");
} else {
    System.out.println("Wheater is bad");
}

// same effect, but much shorter
System.out.println("Wheater is "
        + (wheaterIsGood ? "good" : "bad"));

Switch

switch (digit) {
    case 0: 
        text = "zero";
        break;
        
    case 1:
        text = "one";
        break;
        
    // ...
    
    default:
        text = "???";
        
}

Circles

Do while

do {
    goShopping();
} while (haveTime() && haveMoney());

Foreach

for (String arg : args) {
    System.out.printls(arg);
}

OOP

Constructors

package java.lang;

public final class Integer {
    
    private final int value;
    
    public Integer(int value){
        this.value = value;
    }
    
    // ...
}
package java.math;

public class BigInteger {
    
    public BigInteger(String val){
        this.(val, 10);  // defaul value
    }
    
    public BigInteger(String val, int radix){
        // ...
    }

}

Enums

package java.time;

public enum DayOfWeek {
    MONDAY,  // <- public static final
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;
    
    // fields, methods
}

for (DayOfWeek day : DayOfWeek.values()) {
    System.out.println(day.ordinal() + " " + day.name());
}

Inheritance & Override

package java.lang;

public final class StringBuilder
    extends AbstractStringBuilder {
    
    @Override
    public StringBuilder append(String str){
        // ...
    }
    
    // base method in AbstractStringBuilder:
    // AbstractStringBuilder append(String str)
}

Super

package java.lang;

public final class StringBuilder
    extends AbstractStringBuilder {
    
    public StringBuilder() {
        super(16);
    }
    
    @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }
    
    // ...
}

Extends Object

  • equals()
  • toString()
  • hashCode()

Override equals()

package java.lang;

public final class String /*extends Object*/ {

    @Override
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String other = (String)anObject;
            // ...
        }
        return false;
    }
    // ...
}

Interface

package org.stepic.java.orders;

import java.time.LocalDate;

public interface OrderService {
    Order[] getOrdersByClient(long clientId);
    
    // Order[] getOrdersByClient(long clientId, LocalDate date);
    
    default Order[] getOrdersByClient(
            long clientId, LocalDate date) {
        Order[] allOrders = getOrdersByClient(clientId);
        return Orders.filterByDate(allOrders, date);
    }
}

Read from console

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int a = s.nextInt();
    int b = s.nextInt();
    System.out.println(a + b);
  }
}

Modifiers

// модификаторы protected и private не применимы к классам верхнего уровня
public class ModifiersDemo {
    // доступ отовсюду
    public static void visibleEverywhere() {}

    // доступ только для классов наследников и классов текущего пакета
    protected static void inSubclasses() {}

    // (отсутствие модификатора) доступ только в пределах пакета
    static void inPackage() {}

    // доступ только в пределах класса
    private void inClass() {}
}
Clone this wiki locally