类型转换
public class CharToIntExample {
public static void main(String args[]) {
char c = 'a';
char c2 = '1';
int a = c;
int b = c2;
System.out.println(a);
System.out.println(b);
int d=Character.getNumericValue(c2);
System.out.println(d);
}
}
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToTimestampExample {
public static void main(String args[]) {
Date date = new Date();
Timestamp ts = new Timestamp(date.getTime());
System.out.println(ts);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.format(ts));
}
}
public class IntToStringExample1 {
public static void main(String args[]) {
int i = 200;
String s = String.format("%d", i);
System.out.println(s);
}
}
public class IntToStringExample2 {
public static void main(String args[]) {
int i = 200;
String s = Integer.toString(i);
System.out.println(i + 100);// 300 because + is binary plus operator
System.out.println(s + 100);// 200100 because + is string concatenation operator
}
}
import org.junit.Test;
public class LongToStringExample {
@Test
public void test1() {
long i = 9993939399L;
String s = String.valueOf(i);
System.out.println(s);
}
@Test
public void test2() {
long i = 9993939399L;
String s = Long.toString(i);
System.out.println(s);
}
}
import org.junit.Test;
public class ObjectToStringExample {
@Test
public void test() {
String s = "hello";
StringBuilder sb = new StringBuilder(s);
sb.reverse();
String rev = sb.toString();// converting StringBuilder to String
System.out.println("String is: " + s);
System.out.println("Reverse String is: " + rev);
}
}
import org.junit.Test;
public class StringToFloatExample {
@Test
public void test() {
String s = "23.6";
float f = Float.parseFloat(s);
System.out.println(f);
}
}
public class StringToIntegerExample {
public static void main(String args[]) {
String s = "hello";
int i = Integer.parseInt(s);
System.out.println(i); // NumberFormatException
}
}
public class StringToLongExample {
public static void main(String args[]) {
String s = "9990449935";
long l = Long.parseLong(s);
System.out.println(l);
}
}
public class TestStringToInt {
public static void main(String[] args) {
int a = Integer.parseInt("123");
Integer integer = Integer.valueOf(123);
System.out.println(a + 100);
System.out.println(integer + 100);
}
}