前言
从今日起,陆续分享《HeadFirstJava》的读书笔记,希望能够帮助大家更好的理解Java,提高自己的基础编码能力。
Java是一门面向对象的高级编程语言,常年霸占编程语言排行榜前三。
Java是目前国内的主流开发语言,基本每家大型公司都对Java开发工程师有巨大需求,因此打好Java基本功对我们的职业生涯有很好的帮助。
今天要分享的是【基本概念-进入Java的世界】,希望对大家有所帮助。
书籍精华
Java的工作方式
几个核心概念:源代码,编译器,虚拟机
public class Party {
public void buildInvite() {
Frame f = new Frame();
Label l = new Label("Party at Tim's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
}
}
Java简史
几个重大版本:Java 1.02,Java 1.1,Java2(1.2到1.4),Java5.0(1.5及以上)
- 见过Java 2和Java 5.0,有Java3和Java4么
Java就是这么酷
源文件、类、方法
Java的类
public class MyFirstApp {
public static void main(String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}
public,static,main方法
语句、循环、条件分支
语句:做某件事
循环:反复做某件事
条件分支:在适当的条件下做某件事
Java基本概念
public class IfTest {
public static void main(String[] args) {
int x = 3;
if (x == 3) {
System.out.println("x must be 3");
}
System.out.println("This runs no matter what");
}
}
public class IfTest2 {
public static void main(String[] args) {
int x = 2;
if (x == 3) {
System.out.println("x must be 3");
} else {
System.out.println("x is NOT 3");
}
System.out.println("This runs no matter what");
}
}
万物皆对象
真正的应用
活生生的物联网(智能家居)
public class BeerSong {
public static void main(String[] args) {
int beerNum = 99;
String word = "bottles";
while (beerNum > 0) {
if (beerNum == 1) {
word = "bottle";
}
System.out.println(beerNum + " " + word + " of beer on the wall");
System.out.println(beerNum + " " + word + "of beer.");
System.out.println("Take one down");
System.out.println("Pass it around.");
beerNum = beerNum - 1;
if (beerNum > 0) {
System.out.println(beerNum + " " + word + " of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}
}
}
}
开始编写程序
public class PhraseOMatic {
public static void main(String[] args) {
//你可以随意的加上其他术语
String[] wordListOne = {"24/7", "multi-Tier", "30,000 foot", "B-to-B", "win-win", "front-end", "web-based", "pervasive", "smart", "six-sigma", "critical-path", "dynamic"};
String[] wordListTwo = {"empowered", "sticky",
"value-added", "oriented", "centric", "distributed",
"clustered", "branded", "outside-the-box", "positioned",
"networked", "focused", "leveraged", "aligned",
"targeted", "shared", "cooperative", "accelerated"};
String[] wordListThree = {"process", "tipping-point", " solution", " architecture", " core competency",
"strategy", "mindshare", "portal", "space", "vision",
"paradigm", "mission"};
//计算每一组有多少个名词术语
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length;
//产生随机数字
int rand1 = (int) (Math.random() * oneLength);
int rand2 = (int) (Math.random() * twoLength);
int rand3 = (int) (Math.random() * threeLength);
//组合出专家术语
String phrase = wordListOne[rand1] + " " +
wordListTwo[rand2] + " " + wordListThree[rand3];
//输出
System.out.println("What we need is a " + phrase);
}
}
编译器与Java虚拟机
编译器:将源代码转换成字节码
Java虚拟机:执行字节码
书籍在线地址
代码在线地址
知识点总结
- Java的工作方式:源代码,编译器,Java虚拟机
- Java简史
- Java的程序结构:源文件,类,方法
- main方法
- 语句,分支,循环
- Java虚拟机与编译器的关系
收获
1.idea使用javac命令
https://blog.csdn.net/yasashii/article/details/121221216
2.如何查看class文件字节码文件
1)javap -c
2)使用idea插件:ASM Bytecode Viewer