源码下载:LearnJava-zh.javaJava是一门通用的编程语言,包括并发、基于类的面向对象等特性阅读全文//单行注释/*多行注释*//**JavaDoc(Java文档)注释是这样的。可用于描述类及其属性。*///导入java.util中的ArrayList类importjava.util.ArrayList;//导入java.security包中的所有类importjava.security.*;//每个.java文件包含一个public类,该类名称必须与文件名匹配。publicclassLearnJava{//每个程序都需要有一个main函数作为入口publicstaticvoidmain(String[]args){//使用System.out.println输出到标准输出System.out.println("HelloWorld!”);System.out.println("整数:"+10+"双精度数:"+3.14+"布尔值:"+true);//如果不想输出后自动换行,可以使用System.out.print方法。System.out.print("你好");System.out.print("世界");///////////////////////////////////////类型和变量//////////////////////////////////////使用声明变量//字节类型-8位补码表示//(-128<=byte<=127)bytefooByte=100;//短整数类型-16的补码表示//(-32,768<=short<=32,767)shortfooShort=10000;//整数-32的补码表示//(-2,147,483,648<=integer<=2,147,483,647)intfooInt=1;//Long-64位补码表示//(-9,223,372,036,854,775,808<=long<=9,223,372,036,854,775,807)longfooLong=100000L;//L可以用来表示一个数字是长整数。//其他数字默认为整数。//注意:Java中没有无符号类型//浮点类型——即IEEE754规定的32位单精度浮点类型floatfooFloat=234.5f;//f用来表示一个数是浮点数类型。//否则默认将其视为双精度浮点数。//双精度浮点类型——即IEEE754规定的64位双精度浮点类型doublefooDouble=123.4;//布尔类型-true和falsebooleanfooBoolean=true;布尔barBoolean=false;//字符类型-16位Unicode编码字符charfooChar='A';//使用final使常量不可更改finalintHOURS_I_WORK_PER_WEEK=9001;//stringStringfooString="MyStringIsHere!";//\n表示换行StringbarString="Printingonanewline?\nNoProblem!";//\t表示一个新的制表符字符StringbazString="Doyouwanttoaddatab?\tNoProblem!";System.out.println(fooString);System.out.println(barString);System.out.println(bazString);//数组//数组的大小必须在声明时确定//数组声明格式://<数据类型>[]<变量名>=new<数据类型>[<数组大小>];int[]intArray=newint[10];String[]stringArray=newString[1];布尔[]booleanArray=newboolean[100];//像这样声明和初始化一个数组:int[]intArray={9000,1000,1337};//随机访问数组中的元素System.out.println("intArray@0:"+intArray[0]);//数组索引从0开始,可以改变intArray[1]=1;System.out.println("intArray@1:"+intArray[1]);//=>1//其他数据类型//ArrayLists-类似于数组,但功能更多,并且大小也可以改变//LinkedLists//Maps//HashMaps///////////////////////////////////////////操作员//////////////////////////////////////System.out.println("\n->Operators");inti1=1,i2=2;//多重声明可以简化//算术运算System.out.println("1+2="+(i1+i2));//=>3System.out.println("2-1="+(i2-i1));//=>1System.out.println("2*1="+(i2*i1));//=>2System.out.println("1/2="+(i1/i2));//=>0(向下截断0.5)//取于System.out.println("11%3="+(11%3));//=>2//比较运算符System.out.println("3==2?"+(3==2));//=>falseSystem.out.println("3!=2?"+(3!=2));//=>真System.out.println("3>2?"+(3>2));//=>真System.out.println("3<2?"+(3<2));//=>falseSystem.out.println("2<=2?"+(2<=2));//=>trueSystem.out.println("2>=2?"+(2>=2));//=>true//按位运算符/*~取反,取反Code<>signedrightshift>>>unsignedrightshift&and^exclusiveor|compatibleor*///incrementinti=0;System.out.println("\n->Inc/Dec-rementation");//++和--运算符将变量加或减1,并将其放在变量之前或之后。不同的是整个表达式//的返回值。运算符在前时,先加减,再取值。运算符在后面时,先取值//再加减。System.out.println(i++);//后自增i=1,输出0System.out.println(++i);//预自增i=2,输出2System.out.println(i--);//自减i=1后,输出2System.out.println(--i);//在递减i=0之前,输出0//////////////////////////////////////////控制结构//////////////////////////////////////System.out.println("\n->控制结构");//If语句类似于Cintj=10;if(j==10){System.out.println("我打印出来了");}elseif(j>10){System.out.println("我不");}else{System.out.println("我也不知道");}//While循环intfooWhile=0;while(fooWhile<100){//System.out.println(fooWhile);//递增计数器//遍历99次,fooWhile0->99fooWhile++;}System.out.println("fooWhile值:"+fooWhile);//做而e循环intfooDoWhile=0;做{//System.out.println(fooDoWhile);//增加计数器//遍历99次,fooDoWhile0->99fooDoWhile++;}while(fooDoWhile<100);System.out.println("fooDoWhile值:"+fooDoWhile);//For循环intfooFor;//for循环结构=>for(<起始语句>;<循环条件>;<步长>)for(fooFor=0;fooFor<10;fooFor++){//System.out.println(fooFor);//遍历10次,fooFor0->9}System.out.println("fooForValue:"+fooFor);//SwitchCase语句//switch可用于处理byte、short、char和int数据类型//也可用于处理枚举类型、字符串类和基本数据类型的包装类://Character、Byte,Short,andIntegerintmonth=3;字符串月字符串;switch(month){案例1:monthString="January";休息;案例2:monthString="二月";休息;case3:monthString="三月";休息;默认值:monthString=“其他月份”;休息;}System.out.println("切换案例结果:"+monthString);///////////////////////////////////////类型转换////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////);//返回整数123//将整数转换为字符串Integer.toString(123);//返回字符串"123"//其他数据之间也可以相互转换://Double//Long//String//类型转换//也可以对java对象进行类型转换,但是会涉及到很多概念//可以查看更详细的资料这里://http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html/////////////////////////////////////////具有函数的类///////////////////////////////////////System.out.println("\n->类和函数");//(Bicycle类定义如下)//使用new实例化一个类Bicycletrek=newBicycle();//调用对象的方法trek.speedUp(3);//需要使用getter和setter方法trek.setCadence(100);//toString可以将对象转换为字符串System.out.println("trekinfo:"+trek.toString());}//main方法结束}//LearnJava类结束//你也可以将其他非公共类放入.java文件中//类定义的语法://class{////成员变量、构造函数、函数////Java中的函数称为方法//}classBicycle{//Bicycle类的成员变量和方法publicintcadence;//公共:任何地方都可访问privateintspeed;//private:只能在同一个类中访问protectedintgear;//受保护:在同一个类和子类中都可以访问Stringname;//default:可以在包中访问//构造函数初始化一个对象的方式//下面是一个默认的构造函数publicBicycle(){gear=1;节奏=50;速度=5;名称=“邦特拉格”;}//下面是带参数的构造函数publicBicycle(intstartCadence,intstartSpeed,intstartGear,字符串名称){this.gear=startGear;this.cadence=startCadence;this.speed=startSpeed;this.name=名称;}//函数语法://()//Java类经常使用getters和setters来操作成员变量//方法声明的语法://<作用域><返回值类型><方法名>(<参数列表>)publicintgetCadence(){returncadence;}//void返回值函数没有返回值publicvoidsetCadence(intnewValue){cadence=newValue;}publicvoidsetGear(intnewValue){gear=newValue;}publicvoidspeedUp(intincrement){speed+=increment;}publicvoidslowDown(intdecrement){speed-=decrement;}publicvoidsetName(StringnewName){name=newName;}publicStringgetName(){返回名称;}//返回对象属性的方法@OverridepublicStringtoString(){return"gear:"+gear+“节奏:”+节奏+“速度:”+速度+“名称:”+名称;}}//Bicycle类的结尾//PennyFarthing是BicycleclassPennyFarthingextendsBicycle的子类{//(PennyFarthings是一个非常大的自行车,没有齿轮)publicPennyFarthing(intstartCadence,intstartSpeed){//调用父类的构造函数通过supersuper(startCadence,startSpeed,0,"PennyFarthing");}//你可以使用@注解来表示需要重载的方法//要了解更多注解的使用方法,可以访问以下地址://http://docs.oracle.com/javase/tutorial/java/annotations/@OverridepublicvoidsetGear(intgear){gear=0;}}阅读更多InheritancePolymorphicAbstractExceptionInterfaceGenericJavaCodeSpecification有什么建议吗?还是发现了什么不对?在Github上打开一个问题,或者发起一个pullrequest!原文由JakePrather撰写,经0位好心人修改。译者:ChenboLi?2022JakePrather本作品采用CCBY-SA3.0许可协议授权。