您的位置:首页 > 编程语言 > Java开发

Java基础语法

2016-02-23 19:57 423 查看
内存区分配:

1. code segment:存放代码

2. data segment: 静态变量

3. stack: 局部变量

4. heap: new出来的东西

8种基本数据类型:

布尔型: boolean

字符型: char:1 字节

整数型:

byte:1字节 -127-128

short:2字节 -2 15次方--2 15次方-1

int:4字节

long:8字节

浮点类型:float (4字节, 10的38次方), double(8字节, 10的300次方)

引用数据类型: 类,接口, 数组

double放到float变量里不行, long 放到int里不会出错

数据类型转换:

1. boolean类型不能转换成其他类型

2. 整形int, 字符型char, 浮点型float数据可以相互转换

3. 容量小的类型自动转换为容量大的类型:容量指的是代表的数字的多少, 大小排序为:

byte,short,char-->int-->long-->float-->double(byte,short,char运算时自动转换成int,int+long=long...)

byte, short, char不能互相转换,他们三者在计算时首先自动转换为int类型

4. 容量大的转换为容量小的数据类型要加上强制转换符, 但会造成精度降低或者溢出.

5. 多种类型的数据混合计算时, 系统自动将所有的数据转换成容量最大的那种数据类型, 然后进行计算.

6. 实数常量默认为double, 整形常量默认为int

7. double变量不能强制转换为float, 因为浮点型存的方式不一样

8. float类型要标明f

byte=1 ;  //可以, 只要别超过127
byte b3=b1+b2; //不可以, 要强制转换(byte),改成下面
byte b3=(byte)b1+b2; //如果超过了127, 会自动砍掉前面的字节
float f1=1.23f; //必须加f
float f2=3.2f;
float f3=f1+f2; //不可以, 要强制转换, 因为会变成double


  

public class TestConvert {
public static void main(String arg[]) {
int i1 = 123;
int i2 = 456;
double d1 = (i1+i2)*1.2;//系统将转换为double型运算
float f1 = (float)((i1+i2)*1.2);//需要加强制转换符
byte b1 = 67;
byte b2 = 89;
byte b3 = (byte)(b1+b2);//系统将转换为int型运算,需
//要强制转换符
System.out.println(b3);
double d2 = 1e200;
float f2 = (float)d2;//会产生溢出
System.out.println(f2);

float f3 = 1.23f;//必须加f
long l1 = 123;
long l2 = 30000000000L;//必须加l
float f = l1+l2+f3;//系统将转换为float型计算
long l = (long)f;//强制转换会舍去小数部分(不是四舍五入)

}
}


public class TestConvert2 {
public static void main(String[] args) {

int i=1,j=12;
float f1=(float)0.1;  //0.1f
float f2=123;
long l1 = 12345678,l2=8888888888L;
double d1 = 2e20,d2=124;
byte b1 = 1,b2 = 2,b3 = 127;
j = j+10;
i = i/10;
i = (int)(i*0.1);
char c1='a',c2=125;
byte b = (byte)(b1-b2);
char c = (char)(c1+c2-1);
float f3 = f1+f2;
float f4 = (float)(f1+f2*0.1);
double d = d1*i+j;
float f = (float)(d1*5+d2);
}
}


  

  

char类型相加自动转换int,需要强制转换回来:

public class Test {
public static void main(String[] args){
char c1='c';
char c2=1;
char c=(char)(c1+c2);
System.out.println("c="+c);
//System.out.println("c2="+c2);
}
}


阶乘相加的程序:

public class Test{
public static void main(String[] args){
long result = 0;
long f = 1;
for(int i=1;i<=10;i++){
f=f*i;
result=result+f;
}
System.out.println(result);
}
}


  

1+3+5+...+99:

public class Test {
public static void main(String[] args){
int sum = 0;
for(int i=1; i<=99;i=i+2){
sum=sum+i;
}
System.out.println(sum);
}
}


  

输出0~9:

public class TestWhile{
public static void main(String args[]){
int i=0;
while(i<10){
System.out.println(i);
i++;
}
}
}


while break, continue:

public class Test{
public static void main(String args[]){
int stop=4;
for(int i=1;i<10;i++){
if(i==stop) break;
System.out.println(i);
}
}
}


1-100被3整除的前5位:

public class Test{
public static void main(String args[]){
int num=0;
for(int i=1;i<=100;i++){
if(i%3==0){
System.out.println(i);
num++;
}
if(num==5) break;
}
}
}


  

101-200之间的质数:只能被自身和1整除的数:

public class Test{
public static void main(String args[]){
for(int i=101;i<200;i+=2){
boolean f=true;
for(int j=2;j<i;j++){
if(i%j==0){
f=false;
break;
}
}
if(!f) continue;
System.out.println(i);
}

}
}


switch语句: case可以合并, 小心case穿透, 要使用break, switch里面的变成只能检测int型, 所以可以使用可以转换为int的 char,byte,short类型

public class Test{
public static void main(String args[]){
int i=8;
switch(i){
case(2):
case(3):
case(5):
case(8):
System.out.println("AAAA");
break;
case(9):
System.out.println("bbbb");
break;
default:
System.out.println("bbbb");
}
}
}


  

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: