Java学习编程基础
注释,打印输出,数据类型,String字符串,强制类型转换,赋值运算等
package com.company;
public class javaBasis {
public static void main(String[] args) {
//-----------------------------------------------------------
//注释annotation:(1)单行注释
/*
2)多行注释
*/
/**
* (3)文档注释
*/
//打印输出数据println和print---------------------------------------
// System.out.println("hello world!!!");//输出数据并换行
// System.out.print("welcome");//输出不换行
// System.out.print(" here!");//输出不换行
/*输出结果:
hello world!!!
welcome here!
*/
/*----------------------------------------------------------------
在java中标识符定义的核心原则如下:
由字母、数字、下划线_、$所组成,其中不能使用数字开头,不能使用java中的保留字(关键字)
*/
// int 年龄=20;//从jdk1.7开始增加了中文的支持,即标识符可以使用中文定义(一般不建议使用)
// System.out.println("我今年"+年龄+"岁。");
// int age=21;//一般使用英文单词定义有一定含义的标识符(常用定义方式)
// System.out.println("我今年"+age+"岁。");
/**---------------------------------------------------------------------
* java中的关键字
* abstract......
*/
/**-----------------------------------------------------------
* 数据类型划分
* byte字节、
* short短整型、
* int整形、
* long长整型、
* float单精度型、
* double双精度型、
* char字符型、
* boolean布尔型
*/
//关于初始化内容和默认值-----------------------------------------------------------
// jdk1.5后的变量声明与赋值支持
// int num;//定义变量,未赋值
// num=10;//变量使用前赋值
// System.out.println(num);//输出变量内容
/*输出结果:
10
*/
// int num=10;//jdk1.5之前的版本定义变量方式:在定义变量时赋值初始化的内容
// System.out.println(num);
//整形-----------------------------------------------------------
// int x = 11;//int 变量名称 = 常量;
// System.out.println(x);
/*输出结果:
11
*/
//强制类型转换-----------------------------------------------------------
// int x = 11;
// double y = (double) x;
// System.out.println(y);
/*输出结果:
11.0
*/
//使用var关键字,jdk1.10以后引入,这里是jdk1.8,会报错--------------------
// var x = 10.2;//赋值类型为浮点数
// var y = 10;//赋值类型为整数
// System.out.println(x+"-----"+y);
/*输出结果:
不运行,无结果
*/
//字符型-----------------------------------------------------------
/**
* 大写字母范围:65(‘A’)~90(‘Z’)
* 小写字母范围:97(‘a’)~122(‘z’)
* 数字字符范围:48(‘0’)~57(‘9’)
*/
// char c = 'A';//定义字符变量
// System.out.println(c);//输出字符变量内容
/*输出结果:
A
*/
//char与int转换-----------------------------------------------------------
// char c = 'A';//字符变量
// int num = c;//可以获得字符的编码数值
// System.out.println(num);
/*输出结果:
65
*/
// num = num + 32;//修改编码内容,大小写之间差32
// System.out.println((char) num);//将num转为char
/*输出结果:
a
*/
//使用char可以保存中文-----------------------------------------------------------
// char c = '张三';//报错,只能保存一个汉字
// char c = '张';//一个字符变量
// System.out.println(c);//
/*输出结果:
张
*/
// int num = c;//可以获得字符的编码
// System.out.println(num);
/*输出结果:
24352
*/
//布尔型-----------------------------------------------------------
// boolean flag = true;
// boolean flag = false;
// if (flag){
// System.out.println("true");
// }else {
// System.out.println("false");
// }
/*
flag = true
输出结果:true
flag = false
输出结果:false
*/
//String字符串-----------------------------------------------------------
// String str = "hello world !!!";//使用双引号“”进行描述
// String str1 = "法外狂徒张三";//字符串内容可以为英文也可以是中文
// System.out.println(str+"---"+str1);//输出字符串变量内容
/*输出结果:
hello world !!!---法外狂徒张三
*/
//字符串连接-----------------------------------------------------------
// String str = "hello ";
// String str1 = "world";
// str = str + str1;//字符串连接
// str += " !!!";//字符串连接
// System.out.println(str);//输出字符串内容
/*输出结果:
hello world !!!
*/
//字符”+“号在连接字符串与数值加法计算上的使用--------------------------------
// double x = 10.1;
// int y = 5;
// String str = "计算结果:str="+ x + y;//错误的字符串连接
// System.out.println(str);
// String str1 = "计算结果:str1="+ (x + y);//正确的字符串连接,因为()的优先级更高,会优先计算()内部
// System.out.println(str1);
/*输出结果:
计算结果:str=10.15
计算结果:str1=15.1
*/
//转义字符:换行(\n),制表(\t),\(\\),双引号(\"),单引号(\')-----------
// System.out.println("姓名:\t张三\n年龄:\t26");
/*输出结果:
姓名: 张三
年龄: 26
*/
//运算符-----------------------------------------------------------
// = 赋值运算符
// int x = 10;
// ? : 三目运算符,?前判断得出一个Boolean类型的结果true或false,true则将:前的值赋给变量,false则将:后的值赋给变量
// int y = 10>5?10:5;
//+,-,*,/……
//模运算%,求余数
int x = 10;
x = x % 3;
System.out.println(x);
/*输出结果:
1
*/
//简化赋值运算符-----------------------------------------------------------
/**
* += a+=b等价于a=a+b
* -=
* *=
* /=
* %=
*/
//自增自减机制
/**
* ++,自增,变量值加1
* --,自减,变量值减1
*/
}
}