Java中的String 和char[] ,int和Integer

/***********************************************************/
public class StringVSChar {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";

char[] ch = { 'a', 'b', 'c' };
String s3 = new String(ch);
String s4=new String("abc");

char[] c = s1.toCharArray();
if (s1.equals(s2)) {
System.out.println(true);
} else {
System.out.println(false);
}

if (s1.equals(ch)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (c.equals(ch)) {
System.out.println(true);
} else {
System.out.println(false);

}
if (s1.equals(s3)) {
System.out.println(true);
} else {
System.out.println(false);
}
if (s1.equals(s4)) {
System.out.println(true);
}else {
System.out.println(false);
}
if (s1==s4) {
System.out.println(true);
} else {
System.out.println(false);
}

if (s1==s3) {
System.out.println(true);
} else {
System.out.println(false);
}

}
}
/***********************************************************/
结果:
true
false
false
true
true
false
false
/***********************************************************/
从结果可以看出要弄清几点:
1.String类型可以和char[]进行相互转换,但两种是不同的类型;
2.弄清楚创建的对象在内存中是处于栈还是堆;
3.String中重写了equal(),比较的是两个对象的内容是否一样,如果不重写,和==一样都是比较的对象的引用,new出来的对象都是放在堆中的。
/***********************************************************/
public class IntVSInteger {
public static void main(String[] args) {
Integer i1 = -18;
Integer i2 = -18;

Integer i3=new Integer(-18);
Integer i4=new Integer(-18);

if (i1 == i2) {
System.out.println(true);
} else {
System.out.println(false);
}

if (i1.equals(i2)) {
System.out.println(true);
} else {
System.out.println(false);
}


if (i3==i4) {
System.out.println(true);
} else {
System.out.println(false);
}

if (i3.equals(i4)) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
/***********************************************************/
结果:
true
true
false
true
/***********************************************************/
经调试,在Java中,integer的-128~127是系统预留在栈中的共享数据,这个范围之外的赋值,用==比较就是false.
1. Stringint: - 使用Integer.parseInt方法 ```java String str = "123"; int num = Integer.parseInt(str); ``` - 使用Integer.valueOf方法 ```java String str = "123"; Integer num = Integer.valueOf(str); int n = num.intValue(); ``` 2. intString: - 使用String.valueOf方法 ```java int num = 123; String str = String.valueOf(num); ``` - 使用Integer.toString方法 ```java int num = 123; String str = Integer.toString(num); ``` 3. charString: - 使用String.valueOf方法 ```java char c = 'a'; String str = String.valueOf(c); ``` - 将char转成char数组,再使用String构造方法 ```java char c = 'a'; char[] arr = {c}; String str = new String(arr); ``` 4. Stringchar: - 使用StringcharAt方法 ```java String str = "abc"; char c = str.charAt(0); ``` - 将String转成char数组,再取第一个字符 ```java String str = "abc"; char[] arr = str.toCharArray(); char c = arr[0]; ``` 5. int数组转List: - 使用Arrays.asList方法 ```java int[] nums = {1, 2, 3}; List<Integer> list = Arrays.asList(nums); ``` - 使用IntStream.of方法,再转成List ```java int[] nums = {1, 2, 3}; List<Integer> list = IntStream.of(nums).boxed().collect(Collectors.toList()); ``` 6. List转int数组: - 使用Java 8的stream,再转成int数组 ```java List<Integer> list = Arrays.asList(1, 2, 3); int[] nums = list.stream().mapToInt(Integer::intValue).toArray(); ``` - 使用循环,逐个取出List的元素,再添加到int数组 ```java List<Integer> list = Arrays.asList(1, 2, 3); int[] nums = new int[list.size()]; for (int i = 0; i < list.size(); i++) { nums[i] = list.get(i); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值