![](https://img-blog.csdnimg.cn/20190918140158853.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
Java基础教程(一)
记录学习Java基础
playboy-jordan
磨炼灵魂,提升心智
展开
-
如何写出Java中的第一个 hello world ?(★)
/*需求:练习一个hello world程序。思路:1,定义一个类,因为java程序都定义类中,java程序都是以类的形式存在的,类的形式其实就是一个字节码文件最终体现。2,定义一个主函数。为了让该类可以独立运行。3,因为演示hello world,在控制台上看到该字样,所以需要使用输出语句完成。 步骤:1,用class关键字来完成类的定义,并起一个阅读性强的类名。2,主函数:public static void main(String[] args)这时固定格式的。jvm认识。3,使原创 2020-07-01 20:55:33 · 139 阅读 · 0 评论 -
Java中如何定义基本数据类型的? (★)
class VarDemo { public static void main(String[] args) { //数据类型 变量名 = 初始化值; byte b = 3; short s = 4000; int x = 12; long l = 123l; float f = 2.3f; double d = 3.4; char ch = '1'; boolean bl = true; bl = false;原创 2020-07-02 08:15:52 · 386 阅读 · 0 评论 -
Java中类型转换容易踩到的坑(★★)
class VarDemo2{ public static void main(String[] args) {// int x = 3;// byte b = 5;// x = x + b;// byte b = 3;// b = (byte)(b + 200);//强制类型转换。 //System.out.println((char)('a'+1));// System.out.println('你'+0);//unicode国际标准码表。 b原创 2020-07-02 21:10:16 · 2714 阅读 · 0 评论 -
Java基础之算术运算符(★★)
class OperateDemo { public static void main(String[] args) { //算术运算符。 + - * / %(取余,模运算) +(连接符) // ++(自增:就在原有数据基础上+1,在赋给原有数据) -- //int x = 6370; //x = x / 1000 * 1000; //System.out.println(x);// System.out.println(5%2); // Syst原创 2020-07-02 22:10:12 · 242 阅读 · 0 评论 -
一道关于赋值运算符的很经典的面试题目(★★)
class OperateDemo2 { public static void main(String[] args) { //赋值运算符。= += -= *= /= %=// int a,b,c;// a = b = c = 4; //int a = 4; //a+=2;//a = a + 2; //eg:1 在此例中,s+=4等价于s = (short)(s + 4);但是s+=4不等价于s = s + 4 short s =原创 2020-07-02 22:51:29 · 155 阅读 · 1 评论 -
Java基础之比较运算符(★)
class OperateDemo3{ public static void main(String[] args) { /* 比较运算符,运算完的结果必须是true或者false。 */// System.out.println(3>2);//true// System.out.println(3==2);//false}}原创 2020-07-02 23:14:23 · 3843 阅读 · 0 评论 -
关于逻辑运算符的一道面试题
class OperateDemo3{ public static void main(String[] args) { // 2<x<5 x>2 x<5 /* 逻辑运算符有什么用? 用于连接 两个boolean类型的表达式。 &:与 |:或 &:符号的运算特点: true & true = true; true & false = false; false & tr原创 2020-07-02 23:39:34 · 2779 阅读 · 0 评论 -
Java基础之位运算符和移位运算符
class OperateDemo4 { public static void main(String[] args) {// System.out.println(6&3);// System.out.println(6|3);// System.out.println(~6); System.out.println(3<<2);//3左移两位。 }}位运算符移位运算符原创 2020-07-03 21:56:28 · 3251 阅读 · 1 评论 -
Java基础之位运算符练习
class OperateTest { public static void main(String[] args) { //最有效率的方式算出2乘以8等于几?// System.out.println(2<<3);// 对两个整数变量的值进行互换 (不需要第三方变量) int a = 3,b = 5; System.out.println("a="+a+",b="+b); /* 开发时,使用第三方变量的形式,因为阅读性强。 int c ; c原创 2020-07-03 22:25:53 · 3399 阅读 · 0 评论 -
Java基础之三元运算符
class OperateDemo5 { public static void main(String[] args) { int x = 0,y; y = (x>1)?100:200; System.out.println("y="+y); //获取两个整数中的较大的整数。 int a,b; int max = a>b?a:b; //获取三个整数中的较大的整数。 int o,p,q; int temp = o>p?o:p; i原创 2020-07-03 22:54:36 · 3902 阅读 · 0 评论 -
Java基础之if语句的第一种格式
class IfDemo { public static void main(String[] args) {// System.out.println("Hello World!1");// System.out.println("Hello World!2");// System.out.println("Hello World!3");// System.out.println("Hello World!4"); /* if语句的第一种格式: 1, if(条件原创 2020-07-03 23:12:24 · 2750 阅读 · 0 评论 -
Java基础之if语句面试题目
package test;public class Test { public static void main(String[] args) { /** * 下面的输出语句可以输出吗? * * 可以,因为if(true) System.out.println("yes"); 其实是一个语句, 而 if后面的紧跟的那一个语句是可以省略括号的。 */ if(2==2) if(true)原创 2020-07-03 23:31:54 · 3229 阅读 · 0 评论 -
Java基础之if语句的第二种格式
class IfDemo2 { public static void main(String[] args) { /* if语句的第二种格式: if(条件表达式) { 执行语句; } else//否则 { 执行语句; } */ int x = 1; if(x>1) { System.out.println("yes"); } else { System.out.println("no"); } Sy原创 2020-07-04 08:01:26 · 3424 阅读 · 0 评论 -
Java基础之if语句的第三种格式
class IfDemo3{ public static void main(String[] args) { {//局部代码块可以定义局部变量的生命周期。从而节省虚拟机内存 int a = 3; //a 的运算。 System.out.println(a+4); } /* if语句第三种格式: if(条件表达式) { 执行语句; } else if (条件表达式) { 执行语句; } …… else { 执行语句原创 2020-07-04 08:35:40 · 4584 阅读 · 0 评论 -
Java基础之if练习打印星期天
class IfTest { public static void main(String[] args) { /* 需求:根据用户指定的具体数据,判断该数据对应的星期。 1-星期一Monday 思路: 用户输入无法获取但是那只是具体数据的一种获取手段而已。 而我们要做的功能仅仅是对用户指定的数据进行对应星期的打印而已。 所以具体的数据不确定,完成可以使用变量来表示。 我们只对变量进行操作即可。至于变量的值,可以有用户决定。 因为数据的不确定性,所以要对原创 2020-07-04 08:59:18 · 4787 阅读 · 0 评论 -
Java基础之if练习打印季节
class IfTest2 { public static void main(String[] args) { /* 一年有四季。 春季:3 4 5 夏季:6 7 8 秋季:9 10 11 冬季:12 1 2 根据用户输入的月份,给出对应的季节。 */ int month = 8; if(month<1 || month>12) System.out.println(month+"月没有对应的季节"); else i原创 2020-07-04 09:11:31 · 3793 阅读 · 0 评论 -
Java基础之switch语句
class SwitchDemo { public static void main(String[] args) { /* switch(表达式) { case 取值1: 执行语句; break; case 取值2: 执行语句; break; …... default: 执行语句; break; } */ int x = 2; switch(x)//byte,short,int,char.原创 2020-07-04 09:29:45 · 2117 阅读 · 0 评论 -
Java基础之switch练习
class SwitchTest { public static void main(String[] args) { /* 用户输入的数据对应的出星期。 */ int week = 1; switch(week) { default: break; case 1: System.out.println(week+"对应的是星期一"); break; case 2: System.out.println(week+"对原创 2020-07-04 09:39:11 · 4771 阅读 · 0 评论 -
Java基础之if和switch区别
class { public static void main(String[] args) { ; System.out.println("Hello World!"); }}if和switch的应用:if:1,对具体的值进行判断。2,对区间判断。3,对运算结果是boolean类型的表达式进行判断。switch:1,对具体的值进行判断。2,值的个数通常是固定的。对于几个固定的值判断,建议使用switch语句,因为switch语句会将具体的答案都加载进内存。效率原创 2020-07-04 09:45:46 · 4441 阅读 · 0 评论 -
Java基础之while语句
class WhileDemo{ public static void main(String[] args) { /* while(条件表达式) { 执行语句; } */ int x = 1; while(x<3); { System.out.println("x="+x); x++; } }}原创 2020-07-04 10:00:39 · 184 阅读 · 0 评论 -
Java基础之do while语句
class DoWhileDemo { public static void main(String[] args) { /* do { 执行语句; }while(条件表达式); */ int x = 1; do { System.out.println("x="+x); x++; } while (x<1); /* do while语句的特点:无论条件是否满足,循环体至少执行一次。 */ int y = 1原创 2020-07-04 10:05:31 · 238 阅读 · 0 评论 -
Java基础之while练习累加思想
class WhileTest { public static void main(String[] args) { /* 练习: 获取1到10 10个数字的和。 0 + 1 1 + 2 3 + 3 6 + 4 10 + 5 思路: 1,每次参与加法的数值不确定。 2,每次的出现的和数据也不确定。 3,发现参与加法运算的数值有递增规律。 4,每一次都是加法运算在重复,并且都是和再加上下一个数值。 步骤: 1,原创 2020-07-04 10:22:26 · 4244 阅读 · 0 评论 -
Java基础之计数器思想
class WhileTest { public static void main(String[] args) { /* 练习: 获取1到10 10个数字的和。 0 + 1 1 + 2 3 + 3 6 + 4 10 + 5 思路: 1,每次参与加法的数值不确定。 2,每次的出现的和数据也不确定。 3,发现参与加法运算的数值有递增规律。 4,每一次都是加法运算在重复,并且都是和再加上下一个数值。 步骤: 1,原创 2020-07-04 10:32:36 · 4319 阅读 · 2 评论 -
Java基础之for循环
class ForDemo { public static void main(String[] args) { /* for(初始化表达式;循环条件表达式;循环后的操作表达式) { 执行语句;(循环体) } for(int x = 1; x<3; x++) { System.out.println("x="+x); } */ int x = 1; for(System.out.println("a");x<3; S原创 2020-07-04 10:49:51 · 3959 阅读 · 0 评论 -
Java基础之for循环和while循环区别
class ForTest { public static void main(String[] args) { /* 用for完成累加。 */ int sum = 0; for(int x=1; x<=10; x++) { sum = sum + x; } System.out.println("sum="+sum); /* for和while的特点: 1,for和while可以互换。 2,格式上的不同,在使用上有点小区别。原创 2020-07-04 11:50:59 · 5279 阅读 · 1 评论 -
Java基础之for循环嵌套
class ForForDemo{ public static void main(String[] args) { //大圈套小圈思想。 /* for(int x=0; x<3; x++) { for(int y=0; y<4; y++) { System.out.println("ok"); } } */ /* ***** ***** ***** ***** */ for(int x=0;原创 2020-07-04 13:47:19 · 3897 阅读 · 0 评论 -
Java基础之for循环嵌套练习(一)
class ForForTest { public static void main(String[] args) { /* ***** **** *** ** * */ /* int z = 5; for (int x = 1; x<=5 ;x++ )//1-5 1-4 1-3//1-5 2-5 3-5 { for (int y=1; y<=z ; y++ ) { System.out.print("*");原创 2020-07-04 14:21:08 · 4063 阅读 · 0 评论 -
Java基础之for循环嵌套练习(二)
class ForFor99 { public static void main(String[] args) { /* 九九乘法表 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 */ for (int x=1; x<=9 ;x++ ) { for (int y=1; y<=x ;y++ ) { System.out.print(y+"*"+x+"="+y*x+"\t"); } Syste原创 2020-07-04 16:44:24 · 2980 阅读 · 0 评论 -
Java基础之for循环嵌套练习(三)
class ForForTest2 { public static void main(String[] args) { /* * * * * * -* * * * --* * * ---* * ----* */ for(int x=1; x<=5; x++) { for(int y=1; y<x; y++) { System.out.print(" "); } for(int z=x; z<原创 2020-07-04 17:03:26 · 2473 阅读 · 0 评论 -
Java基础之break和continue
class BreakContinueDemo { public static void main(String[] args) { /* break:跳出。 break作用的范围:要么是switch语句,要么是循环语句。 记住:当break语句单独存在时,下面不要定义其他语句,因为执行不到。 break跳出所在的当前循环。 如果出现了循环嵌套,break想要跳出指定的循环,可以通过标号来完成。 for(int x=0; x<3; x++) {原创 2020-07-04 18:54:56 · 2940 阅读 · 1 评论 -
Java基础之函数的定义和格式
class FunctionDemo{ public static void main(String[] args) {// int a = 3;// int b = 4;// int c;// c = a+b;//// c = 5+7;// c = 9+8; // int c = add(3);// System.out.println("c="+c);// System.out.println("hello world");// myPrin..原创 2020-07-04 21:44:27 · 3603 阅读 · 0 评论 -
Java基础之函数的两个明确
class FunctionDemo3 { public static void main(String[] args) {// draw(4,6);// draw(7,9);// print99(); System.out.println("hello"); } /* 需求1: 定义一个功能,完成两个整数的和的获取。 思路:既然定义功能,就是可以用函数来体现。 如何定义一个函数呢? 通过两个明确来完成。 明确一:这个功能的结果是什么? 是和。是功能原创 2020-07-05 00:35:23 · 3732 阅读 · 0 评论 -
Java基础之函数的重载
/*需求:函数的重载。 */class FunctionDemo4 { public static void main(String[] args) {// add(4,6,9);// System.out.println("Hello World!"); printCFB(7); } /* 函数的重载。 1,同一个类, 2,同名。 3,参数个数不同。or 参数类型不同。 4,函数重载和返回值类型无关。 5,java是严谨性语言,如果函数出现的调用的不确定性.原创 2020-07-05 11:54:27 · 3164 阅读 · 0 评论 -
Java基础之数组及其内存空间
class ArrayDemo { public static void main(String[] args) { //元素类型[] 数组名 = new 元素类型[元素个数或数组长度]; {//局部代码块。限定局部变量的生命周期。 int age = 3; System.out.println(age); } int[] arr = new int[3];// arr[0] = 89; System.out.println(arr[0]);原创 2020-07-05 17:57:38 · 2906 阅读 · 0 评论 -
Java基础之数组常见问题
class ArrayDemo2 { public static void main(String[] args) { //数组的本质就是一个容器 int[] arr = new int[3];// System.out.println(arr[3]);//ArrayIndexOutOfBoundsException: //当访问到数组中不存在的角标时,就会发生该异常。// arr = null;// System.out.println(arr[0]);//Null原创 2020-07-05 18:07:12 · 2320 阅读 · 0 评论 -
Java基础之数组第二种定义和遍历方法
class ArrayDemo3{ public static void main(String[] args) { //格式1 /* 需要一个容器,但是不明确容器的具体数据。 */// int[] arr = new int[3]; /* 需要一个容器,存储已知的具体数据。 */ //元素类型[] 数组名 = new 元素类型[]{元素,元素,……};// int[] arr = new int[]{89,34,270,17}; int[] arr原创 2020-07-05 19:37:15 · 1294 阅读 · 0 评论 -
Java基础之数组求最大值及常见排序算法※※
import java.util.*;class ArrayDemo4 { //遍历数组的功能。 public static void printArray(int[] arr) { System.out.print("["); for(int x=0; x<arr.length; x++) { if(x!=arr.length-1) System.out.print(arr[x]+", "); else System.out.println(ar原创 2020-07-08 22:58:42 · 457 阅读 · 0 评论 -
Java基础之折半查找法※※
import java.util.*;/*面试题:给定一个有序的数组,如果往该数组中存储一个元素,并保证这个数组还是有序的,那么个元素的存储的角标为如何获取。{13,15,19,28,33,45,78,106};*/class ArrayDemo5 { public static void main(String[] args) {//// int[] arr = {4,1,8,7,3,8,2}; int[] arr = {13,15,19,28,33,45,78,106};原创 2020-07-09 21:36:56 · 173 阅读 · 0 评论 -
Java基础之查表法※※
/*获取一个整数的16进制表现形式。*/class ArrayTest2 { public static void main(String[] args) { toHex_2(0); }// 0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F// 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 /* 什么时候使用数组呢? 如果数据出现了对应关系,而且对应关系的一方是有序的数字编号。并作为角标使用。 这时就必须要想到..原创 2020-07-09 23:17:55 · 723 阅读 · 0 评论 -
Java基础之二维数组的定义及内存图解※※
/*二维数组定义的格式。*/class Array2Demo{ public static void main(String[] args) {// int[] arr = new int[3]; // System.out.println(arr);//[I@1fb8ee3 @左边是实体的类型。 @右边是实体的哈希值。// int[][] arr = new int[3][2];//创建一个二维数组,该数组中有3个一维数组,每一个一维数组中有2个元素。// Sy原创 2020-07-10 22:52:33 · 253 阅读 · 0 评论