基本数据类型有8个:整数:byte、int、long、short。 浮点型:float、double。 布尔型boolean。 字符型:char
public static void main(String[] args) {
byte a = 127;
byte b = (byte)(a+1);
System.out.println(b);
}
输出为-128,因为byte类型的范围是-128到127,当byte b = (byte)(a+1);时,a为127;加1之后为128,但是不在范围内,所以返回-128。
Byte(8dit,1字节) short(16bit,2字节) int(32bit,4字节) long(64bit,8字节)
char a = 'b';
char b = 'c';
Sysout.out.print( a + b);
输出为int类型
public class Test {
public static void main(String[] args) {
int i =3287;
int gewei = i % 10;
int shiwei = (i/10) % 10;
int baiwei = (i/10/10) %10;
int qianwei = (i/10/10/10) %10;
System.out.print("个位:" +gewei+ ",十位:" +shiwei+ ",百位:"+baiwei+",千位:"+qianwei);
}
}
public static void main(String[] args) {
int a = 5;
int n = a++;
int b = ++n;
System.out.println(b);
}.
输出为6
public static void main(String[] args) {
int a = 4;
int b = 5;
System.out.println(a
输出为4
public static void main(String[] args) {
int a = -4;
int b = 5;
System.out.println(a & b); //两个操作数,同为1则为1,其余全是0
}
public static void main(String[] args) {
int a = -4;
int b = 5;
System.out.println(a | b); //同为0则是0,其余全是1
}
public static void main(String[] args) {
int a = -4;
int b = 5;
System.out.println(a ^ b); //相同为0,不同为1
}
public static void main(String[] args) {
int a = -4;
System.out.println(a << 2);
}
public static void main(String[] args) {
int a = -4;
System.out.println(a >> 2);
}
输出为4、输出为-3、输出为-7、输出为-16、输出为-1
public static void main(String[] args) {
byte b1 = 1,b2=2,b3,b6;
final byte b4 = 4,b5 = 6;
b6 = b4 + b5;//因为b4,b5被final修饰了,不会自动转化为int类型。
b3 = b1+b2;//因为在进行相加时会将byte转化为int类型,但是b3还是byte类型所以会报错
System.out.println(b3+b6);
}
A:输出结果为:13
B:语句b6 = b4 + b5;编译出错
C:语句b3 = b1+b2;编译出错
D:运行期间抛出异常
C
Length()返回字符串中的长度、equals()判断字符串的值是否相等、x.substring()截取字符串中的内容、x.indexOf()判断是不是字符串的子串、replace替换字符串中的内容
String str1 = "hello";
String str2 = "he" + new String("llo");
System.out.println(str1 == str2);
运行为fales,其中str2是拼接操作创造的新对象,和str1不一样
public class MyAr{
public static void main(String args[]){
int[] i = new int[5];
System.out.println(i[5]);
}
}
会出现异常;因为i的索引为0-4;里面没有索引为5的
int s = 0 ;
for ( int i = 0 ; i < MyArray.length ; i + + )
if ( i % 2 == 1 )
s =s+ MyArray[i] ;
System.out.println(s);
值为300
// String unset="";
String unset=null;
if(unset.length()>0 && unset!=null){
System.out.println("Hello Pido! ");
}
运行时会报错误、应该先判断unset!=null,因为String unset=null;长度为0了,前面的unset.length()>0已经错了,所以不会去判断后面的。
public static void main(String[] args) {
int i = 0;
for(;i<100;i++){
if(i==10){
break; // 结束循环
}
}
System.out.println("i="+i);
}
输出为i=10
String a = "abcdefg";
String b = "abcdefg";
System.out.println(a==b);
输出为true
String a = "abcdefg";
String b = "abc" + "defg";
System.out.println(a==b);
输出为true
(1)根据工龄(整数)给员工涨工资(整数),工龄和基本工资通过键盘录入
(2)涨工资的条件如下: [10-15) +5000 [5-10) +2500 [3~5) +1000 [1~3) +500 [0~1) +200
(3)如果用户输入的工龄为10,基本工资为3000,程序运行后打印格式"您目前工作了10年,基本工资为 3000元, 应涨工资 5000元,涨后工资 8000元"
(1)定义一个int类型的一维数组,内容为{6,2,9,15,1,5,20,7,18}
(2)将数组最大元素与最后一位元素进行交换,最小元素与第一位元素进行交换,并打印数组 提示思路:先查找最大值和最小值出现的索引。
class Person{
private int id;
private String name;
public Person(int id,String name) {
this.id = id;
this.name = name;
}
}
public class StudentClass {
public static void main(String[] args) {
Person a = new Person(23, "a");
Person b = new Person(22,"b");
swap(a, b);
System.out.println(a == a);
System.out.println(b == b);
}
private static void swap(Person a, Person b) {
Person temp = a;
a = b;
b = temp;
}
}
true、true