赞
踩
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
int age = 25;
String name = "John Doe";
double salary = 5000.50;
boolean isEmployed = true;
package com.example.mypackage;
public class MyClass {
// class implementation
}
public class Animal {
String name;
int age;
public void eat() {
System.out.println("Animal is eating");
}
}
public class Dog extends Animal {
String breed;
public void bark() {
System.out.println("Dog is barking");
}
}
public interface MyInterface {
public void method1();
public int method2(String str);
}
public class MyClass implements MyInterface {
public void method1() {
System.out.println("Method 1 implementation");
}
public int method2(String str) {
return str.length();
}
}
public class Animal { public void makeSound() { System.out.println("Animal is making a sound"); } } public class Dog extends Animal { public void makeSound() { System.out.println("Dog is barking"); } } public class Cat extends Animal { public void makeSound() { System.out.println("Cat is meowing"); } } Animal myAnimal = new Dog(); myAnimal.makeSound();
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("Finally block executed");
}
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
MyThread t = new MyThread();
t.start();
List<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("orange"); System.out.println(list); Set<String> set = new HashSet<>(); set.add("apple"); set.add("banana"); set.add("orange"); System.out.println(set); Map<String, Integer> map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("orange", 3); System.out.println(map); Queue<String> queue = new LinkedList<>(); queue.add("apple"); queue.add("banana"); queue.add("orange"); System.out.println(queue);
Class<?> c = Class.forName("com.example.MyClass");
Object obj = c.newInstance();
Method method = c.getMethod("myMethod");
method.invoke(obj);
@MyAnnotation(value = "someValue")
public class MyClass {
// class implementation
}
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
list.forEach((String s) -> System.out.println(s));
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
Stream<String> stream = list.stream();
stream.forEach((String s) -> System.out.println(s));
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
for (String s : list) {
System.out.println(s);
}
public enum DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
DayOfWeek today = DayOfWeek.MONDAY;
System.out.println(today);
try {
FileWriter writer = new FileWriter("myfile.txt");
writer.write("Hello, world!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
public class MyClass implements Serializable { private String name; private int age; public MyClass(String name, int age) { this.name = name; this.age = age; } // getters and setters } MyClass obj = new MyClass("John Doe", 25); try { FileOutputStream fileOut = new FileOutputStream("myfile.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(obj); out.close(); fileOut.close(); } catch (IOException e) { e.printStackTrace(); }
String str = "Hello, world!";
Pattern pattern = Pattern.compile("Hello");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("Pattern found");
}
try {
Socket socket = new Socket("localhost", 8080);
OutputStream out = socket.getOutputStream();
out.write("Hello, world!".getBytes());
out.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
String input = request.getParameter("input");
if (!isValid(input)) {
throw new IllegalArgumentException("Invalid input");
}
String plainText = "Hello, world!";
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(plainText.getBytes());
byte[] digest = md.digest();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "username", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
while (rs.next()) {
// process result set
}
rs.close();
stmt.close();
conn.close();
import org.junit.Test;
import static org.junit.Assert.*;
public class MyClassTest {
@Test
public void testMyMethod() {
MyClass obj = new MyClass();
int result = obj.myMethod();
assertEquals(10, result);
}
}
import java.util.logging.*;
public class MyClass {
private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName());
public void myMethod() {
LOGGER.info("Method called");
}
}
@Entity @Table(name = "mytable") public class MyClass { @Id private Long id; private String name; // getters and setters } EntityManagerFactory emf = Persistence.createEntityManagerFactory("mydatabase"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); MyClass obj = new MyClass(); obj.setName("John Doe"); em.persist(obj); em.getTransaction().commit(); em.close(); emf.close();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。