我爱编程

三天学会基本Java语法— Day 1(Jav

2016-03-31  本文已影响9793人  无状态Rio

三天基本学会Java,挑战不可能



今天我们要完成的任务


Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented,and specifically designed to have as few implemen-tation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA),meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. As of 2016, Java is one of the most popular programming languages in use, particularly for client-server web applications, with a reported 9 million developers.Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.


A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program. There are three notions of the JVM: specification, implementation, and instance. The specification is a document that formally describes what is required of a JVM implementation. Having a single specification ensures all implementations are interoperable. A JVM implementation is a computer program that meets the requirements of the JVM specification. An instance of a JVM is an implementation running in a process that executes a computer program compiled into Java bytecode.

JVM机制

和任何一个语言一样我们从Hello World开始

public class HelloWorld {
   /* 第一个Java程序
     *它将打印字符串 Hello World
    */
 public static void main(String []args) {
    System.out.println("Hello World"); // 打印 Hello World
}

Java中这些特点是我们从一开始就必须要注意的

大小写敏感:Java是大小写敏感的,这就意味着标识符A与a是不同的。
类名:对于所有的类来说,类名的首字母应该大写。如果类名由若干单词组成,那么每个单词的首字母应该大写,例如 MyFirstJavaClass 。
方法名:所有的方法名都应该以小写字母开头。如果方法名含有若干单词,则后面的每个单词首字母大写。
源文件名:源文件名必须和类名相同。当保存文件的时候,你应该使用类名作为文件名保存(切记Java是大小写敏感的),文件名的后缀为.java。(如果文件名和类名不相同则会导致编译错误)。


下面列出了Java保留字。这些保留字不能用于常量、变量、和任何标识符的名称。

关键字 描述
abstract 抽象方法,抽象类的修饰符
assert 断言条件是否满足
boolean 布尔数据类型
break 跳出循环或者label代码段
byte 8-bit 有符号数据类型
case switch 语句的一个条件
catch 和try搭配扑捉异常信息
char 16-bit Unicode字符数据类型
class 定义类
const 未使用
continue 不执行循环体剩余部分
default switch语句中的默认分支
do 循环语句,循环体至少会执行一次
double 64-bit双精度浮点数
else if 条件不成立时执行的分支
enum 枚举类型
extends 表示一个类是另一个类的子类
final 表示一个值在初始化之后就不能再改变了表示方法不能被重写,或者一个类不能有子类
finally 为了完成执行的代码而设计的,主要是为了程序的健壮性和完整性,无论有没有异常发生都执行代码。
float 32-bit单精度浮点数
for for循环语句
goto 未使用
if 条件语句
implements 表示一个类实现了接口
import 导入类
instanceof 测试一个对象是否是某个类的实例
int 32位整型数
interface 接口,一种抽象的类型,仅有方法和常量的定义
long 64位整型数
native 表示方法用非java代码实现
new 分配新的类实例
package 一系列相关类组成一个包
private 表示私有字段,或者方法等,只能从类内部访问
protected 表示字段只能通过类或者其子类访问子类或者在同一个包内的其他类
public 表示共有属性或者方法
return 方法返回值
short 16位数字
static 表示在类级别定义,所有实例共享的
strictfp 浮点数比较使用严格的规则
super 表示基类
switch 选择语句
synchronized 表示同一时间只能由一个线程访问的代码块
this 表示调用当前实例或者调用另一个构造函数
throw 抛出异常
throws 定义方法可能抛出的异常
transient 修饰不要序列化的字段
try 表示代码块要做异常处理或者和finally配合表示是否抛出异常都执行finally中的代码
void 标记方法不返回任何值
volatile 标记字段可能会被多个线程同时访问,而不做同步
while while循环

Java注释

类似于C/C++,Java也支持单行以及多行注释。注释中的字符将被Java编译器忽略。

注释示范

 //单行注释
 /*多行注释
  *多行注释第二行
  *多行注释第三行*/

类和对象

说到Java,必须从类开始说,以下是Java作为一门面向对象语言的基本特征

接下来举个栗子来说明下类

public class Person{
String name;
int age;
boolean sex;
void eating(){ //方法一
     } 
void running(){ //方法二
    } 
void sleeping(){
   }
}

方法在类之中,如eating是Person类的方法。

接下来说明下构造方法

class Name{
public sam(){ //默认了构造方法
System.out.println("Sam"); 
}

每个类都有构造方法。如果没有显式地为类定义构造方法,Java编译器将会为该类提供一个默认构造方法

指定构造方法

class Name{
public sam(String name){ //指定了构造方法,构造器只有一个参数name
System.out.println("Sam"); 
}

之后就可以直接说如何构造对象了

public class Car{
  public Puppy(String name){
      //这个构造器仅有一个参数:name
     System.out.println("My car brand is :" + name ); 
  }
   public static void main(String []args){
      // 下面的语句将创建一个Car对象
      Puppy myPuppy = new Car( "Benz" );
   }
}

编译运行上面的代码,运行结果将是

 My car brand is :Benz

创建对象

对象是根据类创建的。在Java中,使用关键字new来创建一个新的对象。创建对象需要按照这样的三步走

嗯,没有栗子不能愉快的说明对不对

下面是个完整的栗子

从头开始构建Car类

public class Car {
String brandname;

public Car(String name){
// 这个构造器有且仅有一个参数:name
System.out.println("Car's brand is:"+ name);
}

public void setbrand( String name ){
brandname = name;
/void(空)方法不需要返回值/
}

 public String getname(){
     System.out.println("Brand's name is :" + brandname ); 
    return brandname;
    /*设定为String类型的方法必须返回String类型的值*/
      }
 public static void main(String[] args) {
    //创建对象 
    Car c = new Car("BMW");     
    //通过方法来设定车名
    c.setbrand("BMW");
    //调用另一个方法获取
    c.getname( );
    //你也可以像这样直接访问成员变量
    System.out.println("String Value :" + c.brandname);         
     }
 }

编译运行之,得结果如下

Car's brand is:BMW
Brand's name is :BMW
String Value :abc

Java包

包主要用来对类和接口进行分类。当开发Java程序时,可能编写成百上千的类,因此很有必要对类和接口进行分类。

Import语句

在Java中,如果给出一个完整的限定名,包括包名、类名,那么Java编译器就可以很容易地定位到源代码或者类。Import语句就是用来提供一个合理的路径,使得编译器可以找到某个类。

源文件声明规则

源文件声明是有规则的。当在一个源文件中定义多个类,并且还有import语句和package语句时,要特别注意这些规则。

*类有若干种访问级别,并且类也分不同的类型:抽象类和final类等。这些将在后面两天中涉及到。除了上面提到的几种类型,Java还有一些特殊的类,如:内部类、匿名类。

预告:Day2的主角是运算和各种结构~

上一篇 下一篇

猜你喜欢

热点阅读