public class Main {
public static void main(String[] args) {
float f = 0.0f;
for (int i=1; i <= 7; i++) {
f += 0.1111111111111111;
}
System.out.println(f);
}
}
Результат: 0.7777778
public class Main {
public static void main(String[] args) {
double f = 0.0f;
for (int i=1; i <= 7; i++) {
f += 0.1111111111111111;
}
System.out.println(f);
}
}
Результат: 0.7777777777777779
public class Main {
public static void main(String[] args) {
//прибавляем к нулю 0.1 одиннадцать раз подряд
double f1 = 0.0;
for (int i = 1; i <= 11; i++) {
f1 += .1;
}
//Умножаем 0.1 на 11
double f2 = 0.1 * 11;
//должно получиться одно и то же - 1.1 в обоих случаях
System.out.println("f1 = " + f1);
System.out.println("f2 = " + f2);
// Проверим!
if (f1 == f2)
System.out.println("f1 и f2 равны!");
else
System.out.println("f1 и f2 не равны!");
}
}
f1 = 1.0999999999999999
final double threshold = 0.0001;
if (Math.abs(f1 - f2) < threshold)
System.out.println("f1 и f2 равны");
else
System.out.println("f1 и f2 не равны");
public class HelloWorld {
public static void say(String str) {
System.out.println(str);
}
public static void main(String[] args) {
say("Hello");
say("World!");
}
}
Manifest-version: 1.0 Main-Class: HelloWorld
javac HelloWorld.java jar cfm hw.jar manifest.mf HelloWorld.class java -jar hw.jar
package ru.petrsu.cross;
public class Say {
public void say(String str) {
System.out.println(str);
}
}
import ru.petrsu.cross.Say;
public class HelloWorld {
public void main(String[] args) {
Say s = new Say();
s.say("Hello");
s.say("World!");
}
}
package ru.petrsu.cross;
public class Say {
public void say(Anymal animal) {
System.out.println(animal.say());
}
}
package ru.petrsu.cross;
abstract public class Animal {
abstract public String say();
}
package ru.petrsu.cross;
public class Dog extends Animal {
public String say() {
return "Hav";
}
}
import ru.petrsu.cross.Say;
import ru.petrsu.cross.Dog;
public class HelloWorld {
public static void main(String[] args) {
Say s = new Say();
s.say(new Dog());
}
}
public class Main {
private Object obj = new Object();
public void doSomething() {
//...какая-то логика, доступная для всех потоков
synchronized (obj) {
//логика, которая одновременно доступна только для одного потока
}
}
}
class Dog implements Serializable
{
public String name;
public int age;
public int weight;
}
public static void main(String[] args) throws Exception
{
Dog dog = new Dog();
//save cat to file
FileOutputStream fileOutput = new FileOutputStream("dog.dat");
ObjectOutputStream outputStream = new ObjectOutputStream(fileOutput);
outputStream.writeObject(dog);
fileOutput.close();
outputStream.close();
//load cat from file
FileInputStream fiStream = new FileInputStream("dog.dat");
ObjectInputStream objectStream = new ObjectInputStream(fiStream);
Object object = objectStream.readObject();
fiStream.close();
objectStream.close();
Dog newDog = (Dog)object;
}
class Dog implements Serializable
{
public String name;
public int age;
public int weight;
transient public InputStream in = System.in;
}
package ru.petrsu.cross;
public class JNIHelloWorld {
native void printHelloWorld();
}
/* DO NOT EDIT THIS FILE - it is machine generated */ #includeГенерируется из исходников: javah ru.forwolk.test.JNIHelloWorld/* Header for class ru_forwolk_test_JNIHelloWorld */ #ifndef _Included_ru_forwolk_test_JNIHelloWorld #define _Included_ru_forwolk_test_JNIHelloWorld #ifdef __cplusplus extern "C" { #endif /* * Class: ru_forwolk_test_JNIHelloWorld * Method: printHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_ru_forwolk_test_JNIHelloWorld_printHelloWorld (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
#includeКомпиляция: g++ -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -fPIC JNIHelloWorld.cpp -shared -o helloworld.so -Wl,-soname -Wl,--no-whole-archive#include "JNIHelloWorld.h" JNIEXPORT void JNICALL Java_ru_forwolk_test_JNIHelloWorld_printHelloWorld (JNIEnv *, jobject) { std::cout << "Hello world!"; }
package ru.petrsu.cross;
public class JNIHelloWorld {
native void printHelloWorld();
static {
System.load(System.getProperty("PROJECT_ROOT") + "/bin/helloworld.so"); //Absolute path!
}
}
public static void main(String[] args) {
JNIHelloWorld p = new JNIHelloWorld();
p.printHelloWorld();
}
interface Animal {
public String say();
}
public class Dog implements Animal {
public String say() {
return 'Hav';
}
}
package lombok;
public class Chelovek {
private String name;
private int age;
public Chelovek(String name, int age) {
this.name = name;
this.age = age;
}
public Chelovek() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package lombok;
public class Chelovek {
@Getter @Setter private String name;
@Getter @Setter private int age;
}
package lombok;
@Data
public class Chelovek {
private String name;
private int age;
}
@Data - @ToString, @EqualsAndHashCode, @Getter на все поля, @Setter на все не final поля, и @RequiredArgsConstructor!