class MyRunnable implements Runnable{
@Override
public void run(){
for (int i = 0; i < 500; i++){
System.out.print('*');
}
}
}
public class MyApp {
public static void main(String[] args){
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
for (int i = 0; i < 500; i++){
System.out.print('.');
}
}
}
[/java]
[vagrant@localhost java]$ java MyApp
..............................................................................................................................................******************************************************************************...................................................................................................................................................**********************************************************************************************************************************************************************************************************************************************...................................................................................................................................................................................................................****************************************************************************************************************************************************************************************
String class
[java]
public class MyApp {
public static void main(String[] args){
String s = "abcdef";
System.out.println(s.length());
System.out.println(s.substring(2, 5));
System.out.println(s.replaceAll("ab", "AB"));
String s1 = "ab";
String s2 = "ab";
if (s1.equals(s2)){
System.out.println("same!");
}
String ss1 = new String("ab");
String ss2 = new String("ab");
if(ss1 == ss2){
System.out.println("same!same!same!");
}
}
}
[/java]
[java]
public class MyApp {
public static void main(String[] args){
int score = 50;
double height = 165.8;
String name = "yokoi";
System.out.printf("name: %s, score: %d, height: %f\n", name, score, height);
System.out.printf("name: %-10s, score: %10d, height: %5.2f\n", name, score, height);
}
}
[/java]
[java]
public class MyApp {
public static void main(String[] args){
double d = 53.234;
System.out.println(Math.ceil(d));
System.out.println(Math.floor(d));
System.out.println(Math.round(d));
System.out.println(Math.PI);
}
}
[/java]
[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
54.0
53.0
53
3.141592653589793
Random処理
[java]
Random r = new Random();
System.out.println(r.nextDouble());
System.out.println(r.nextInt(100));
System.out.println(r.nextBoolean());
[/java]
[java]
import java.util.*;
public class MyApp {
public static void main(String[] args){
// ArrayList
List
sales.add(10);
sales.add(20);
sales.add(30);
for(int i = 0; i < sales.size(); i++){ System.out.println(sales.get(i)); } sales.set(0, 100); sales.remove(2); for(Integer sale : sales){ System.out.println(sale); } } } [/java] [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 10 20 30 100 20