java小白学习中03
3.类型转换
①java是强类型语言,进行运算时,需要用到类型转换
低---------------------------------高
byte,short,char->int->long->float->double
②注意:a.强制转换 (类型)变量名 由高向低转换;自动转换 由低向高转换
b.不能对布尔值进行转换
c.转换时可能存在内存溢出或精度问题
1 public class Demo01 { 2 public static void main(String[] args) { 3 int i = 128; 4 byte a = (byte)i;//内存溢出 5 double b = i; 6 //强制转换 (类型)变量名 高到低转换 7 //自动转换 由低向高转换 8 System.out.println(i);//128 9 System.out.println(a);//-128(内存溢出) 10 System.out.println(b);//128.0 11 System.out.println((int)23.58);//23(精度问题) 12 System.out.println((int)-58.236F);//-58(精度问题) 13 /* 14 注意: 15 1.不能对布尔值进行转换 16 2.不能把转换对象换成不相干的类型 17 3.高容量转换成低容量时,需要强制转换 18 4.转换时可能存在内存溢出,或者精度问题 19 */ 20 char c = 'A'; 21 int d = c+1; 22 System.out.println(d);//66 23 System.out.println((char)d);//B
③常见问题:
public class Demo01 { public static void main(String[] args){ /*常见问题: 1.操作比较大的数时注意溢出问题 2.数字之间可以用下划线分割开 */ int money = 10_0000_0000; int year = 20; int total = money*year;//-1474836480 ,计算时已经溢出 long total1 = money*year;//默认是int,转化之前已经出现问题 long total2 = ((long)money)*year;//20000000000 } }
4.变量
数据类型 变量名 = 值;可以用逗号隔开来声明多个同类型变量。
public class Demo03 { public static void main(String[] args) { //int a,b,c; //int a=1,b=2,c=3;程序可读性 String name = "yunyan"; char a = 'X'; double b = 3.14; } }
②
-
每个变量都有类型,包括基本类型和引用类型。
-
变量名必须是合法的标识符。
-
public class Demo04 { //类变量 static static double salary = 2500; //属性:变量 /*实例变量:从属于对象;如果不自行初始化,默认为0 0.0 u0000 布尔值:默认为flase ; 除了基本类型,其他都是null */ String name; int age; //main方法 public static void main(String[] args) { //局部变量,必须声明和初始化值 int i = 10; System.out.println(i);//10 //变量类型 变量名字= new Demo04(); Demo04 Demo04 = new Demo04();//alt+enter自动补全 System.out.println(Demo04.age);//0 System.out.println(Demo04.name);//null System.out.println(salary);//2500.0 } //其他方法 eg:add方法 public void add(){ } }
④变量的命名规范
-
所有的变量,方法,类名:见名知意
-
类成员变量:首字母小写和驼峰原则:除第一个单词以外,后面的单词首字母大写 eg : lastName
-
局部变量:首字母小写和驼峰原则
-
常量:大写字母和下划线:MAX_VELUE
-
-
方法名:首字母小写和驼峰原则:run(),runRun()
5.常量:初始化后不能再改变的量;常量名一般使用大写符号。
final 常量名=值; final double PI=3.14;
public class Demo03 { //static final 属于修饰符,不分前后顺序 //常量名需要大写字母 static final double PI = 3.14; public static void main(String[] args) { System.out.println(PI); } }
6.运算符
①基本运算符
%:表示取余;==:表示等于;!=:表示不等于;&&:与;||:或;!:非;

public class Demo01 { public static void main(String[] args) { //二元运算符 int a = 10; int b = 20; int aa = 21; System.out.println(a+b);//30 System.out.println(a-b);//-10 System.out.println(a*b);//200 System.out.println(a/b);//0 System.out.println(a/(double)b);//0.5 System.out.println(aa%a);//1 取余 模运算 aa / a = 2...1 /*不同类型之间运算 如果里面有long,结果为long类型;如果没有long,结果为int类型 */ long c = 12345678L; int d = 123; short e = 10; byte f = 8; System.out.println(c+d+e+f);//12345819 long类型 System.out.println(d+e+f);//141 int类型 System.out.println(e+f);//18 int类型 } }
public class Demo02 { public static void main(String[] args) { //关系运算符:结果只有true与false 布尔值 //if int a = 10; int b = 20; System.out.println(a>b);//false System.out.println(a<b);//true System.out.println(a==b);//false System.out.println(a!=b);//true } }
②自增自减运算符 ++ --
public class Demo03 { public static void main(String[] args) { //++ -- 自增 自减 属于一元运算符 int a = 10; int b = a++;//a++:a=a+1 执行这行代码前先赋值给b,然后再自增 // a++ a = a+1; System.out.println(a);//11 //++a a =a+1; int c = ++a;//++a:a=a+1 先自增,再赋值给c System.out.println(a);//12 System.out.println(b);//10 System.out.println(a);//12 //幂运算 2*2*2=8 Math.pow(2,3) 使用工具类来操作 double pow = Math.pow(2, 3); System.out.println(pow);//8.0 } }
③逻辑运算符 &&:与(and) ||:或(or) !:非(取反)
public class Demo04 { public static void main(String[] args) { /*逻辑运算符 &&:与(and) ||:或(or) !:非(取反) */ boolean a = true; boolean b = false; System.out.println("a && b:"+(a&&b));//false 逻辑与运算:一假全假 System.out.println("a || b:"+(a||b));//true 逻辑或运算:一真全真 System.out.println("!(a && b):"+!(a&&b));//true 如果是真,则为假;如果是假,则为真。 //与运算举例 短路运算 int c = 10; boolean d = (c<4)&&(c++<4);//若&&前面为假时,则直接为假,不用进行后面的程序 System.out.println(d);//false System.out.println(c);//10 } }
④位运算符 & | ^ ~ << >>
public class Demo05 { public static void main(String[] args) { /*位运算 & | ^ ~ << >> A = 0011 1100; 二进制 B = 0000 1101; ---------------------------------------- A&B = 0000 1100 ; 上下对应有0则为0 A|B = 0011 1101 ; 上下对应有1则为1 A^B = 0011 0001 ; 同为0,异为1 ~B = 1111 0010 ; 取相反 */ /* << 左移 相当于*2 >> 右移 相当于/2 ------------------------------------------- 0000 0000 0 0 0 0 0 0 0 0 0 0000 0001 1 128 64 32 16 8 4 2 0(1) 0000 0010 2 0000 0011 3 0000 0100 4 0000 0101 5 0000 0110 6 0000 0111 7 0000 1000 8 0001 0000 16 ------------------------------------- */ System.out.println(2<<3);//16 } }
⑤条件运算符 ? :
public class Demo06 { public static void main(String[] args) { /*三元运算符: ? : x ? y : z ; 如果x为ture,则为y;如果x为false,则为z。 */ int score = 80; int type = score<60 ? 30 : 20; String type01 = score>60 ? "及格" : "不及格" ; System.out.println(type);//20 System.out.println(type01);//及格 //优先级 () } }
⑥扩展运算符 += -= *= /=
public class Demo06 { public static void main(String[] args) { //拓展运算符 += -= *= /= int a = 10; int b = 20; a += b ;//a = a+b; a -= b ;//a = a-b; System.out.println(a);//10 //字符串连接符 + ""String 字符串 如果字符串在前面,拼接;如果在后面直接运算 System.out.println(""+a+b);//1020 System.out.println(a+b+"");//30 } }
7.包机制
- 用于区别类名的命名空间,本质等同于文件夹
- 包语句的语法格式为:
package pkg1[. pkg2[. pkg3...]];
eg : com.chen.base com.yunyan.xxx
- 为了能够使用某一个包的成员,我们需要在java程序中明确导入该包,使用import完成此功能
import package1[.package2...].(classname|*);![]()
package com.chen.operator; //导入这个包下所有的类 import com.chen.base.*; //导入其他包下的单个类 import com.chen.base.Demo; public class Demo06 { public static void main(String[] args) { Demo//输入类名称,双击左键 } }
8.
package com.chen.base; //注释需加在想要显示的上面 /** * @author yunyan * @version 1.0 * @since 1.8 */ public class Doc { //属性 String name; //test 方法 /** * @author yunyan * @param name * @return * @throws Exception */ public String test(String name) throws Exception{ return name; } //通过命令行 javadoc 参数 java文件 //作业:学会查找使用IDEA生产javaDoc文档 呀 作业不会写。。。 }




浙公网安备 33010602011771号