20145109 《Java程序设计》第三周学习总结

教材学习内容总结

Chapter 4 Object

4.1 Class & Object

definition of class:

class Clothes {
String color;
char size;
//color & size are called field member
}

foundation of an instance:

new Clothes();

reference:

Clothes c1 = new Clothes();

Constructor:

class Clothes {
String color;
char size; Clothes2(String color, char size) {
this.color = color;
this.size = size;
}
}

java.util.Scanner:

import  java.util.Scanner;

public class Guess {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = (int) (Math.random() * 10);
int guess; do {
System.out.print("猜数字(0~9):");
guess = scanner.nextInt();
} while (guess != number); System.out.println("猜中了");
}
}

Aside from nextInt(), there're other methods like: nextByte(), nextShort(), nextLong(), nextFloat(), nextDouble(), nextBoolean(), nextLine()

java.math.BigDecimal:


import java.math.BigDecimal; public class DecimalDemo {
public static void main(String[] args) {
BigDecimal operand1 = new BigDecimal("1.0");
BigDecimal operand2 = new BigDecimal("0.8");
BigDecimal result = operand1.subtract(operand2);
System.out.println(result);
}
}

+, -, *, / -> plus(), substract(), multiply(), divide()

equals(): judge equation

equation & same:

'==': the same object

'.equals()': value equation

4.2 Basic Type of Wrapper

Long, Integer, Double, Float, Boolean

public class IntegerDemo {
public static void main(String[] args) {
int data1 = 10;
int data2 = 20;
Integer wrapper1 = new Integer(data1);
Integer wrapper2 = new Integer(data2);
System.out.println(wrapper1.doubleValue()/3);
System.out.println(wrapper1.compareTo(wrapper2));
}
}

Autoboxing & Auto unboxing

Integer wrapper = 10;
int foo = wrapper;

4.3 Array, an Object

public class Array_XY {
public static void main(String[] args) {
int[][] cords = {
{1, 2, 3},
{4, 5, 6}
}; for (int x = 0; x < cords.length; x++) {
for (int y = 0; y < cords[x].length; y++) {
System.out.printf("%2d", cords[x][y]);
}
System.out.println();
} /* enhanced for loop:
for (int[] row : cords) {
for (int value : row) {
System.out.printf("%2d", value);
}
System.out.println();
}
*/ }
}

operating arrays

int[] scores = new int[10];

//or
int[] scores = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

import java.util.Arrays:

Arrays.fill(arrayName, value);

irrgular array

int[][] arr = new int[2][];
arr[0] = new int[] {1, 2, 3, 4, 5};
arr[1] = new int[] {1, 2, 3};

class-type array

Integer[] scores = new Integer[3];

This sentence build none object, because every index is refered to null.

array copy

int[] scores1 = {...}
int[] scores2 = new int[scores1.length];
for (int i = 0; i < scores1.length; i++) {
scores2[i] = scores1[i];
}

int[] scores1 = {...}
int[] scores2 = new int[scores1.length];
System.arraycopy(scores1, 0, scores2, 0, scores1.length);

import java.util.Arrays

int[] scores1 = {...}
int[] scores2 = Arrays.copyOf(scores1, scores1.length);

In Java, once an array is set up, the length is fixed. The only way to change it is to build another array.

int[] scores2 = Arrays.copyOf(scores1, scores1.length * 2);

deep copy & shallow copy

No matter System.arraycopy() or Arrays.copyOf(), they are all shallow copy. When to copy the object, you must operate yourself.

Clothes2[] c1 = {new Clothes2("red", 'L'), new Clothes2("blue", 'M')};
Clothes2[] c2 = new Clothes2[c1.length]; for (int i = 0; i < c1.length; i++) {
Clothes2 c = new Clothes(c1[i].color, c1[i].size);
c2[i] = c;
}

4.4 String, an Object

String name = "justin";
System.out.println(name);
System.out.println(name.length());
System.out.println(name.charAt(0));
System.out.println(name.toUpperCase()); char[] cs = {'j', 'u', 's', 't', 'i', 'n'};
String name = new String(cs); char[] cs2 = name.toCharArray(); System.out.println("your name is: " + name);

String to number:

Integer.parseInt(...)
Double.parseDouble(...)
...

String Characteristic :

  1. String constant & String pool
  2. Immutable String

String literal & String pool

char[] name = {'J', 'u', 's', 't', 'i', 'n'};
String name1 = new String(name);
String name2 = new String(name);
System.out.println(name1 == name2);

Abviously, the ansewer is 'false'.

What about the following slice?

String name1 = "Justin";
String name2 = "Justin";
System.out.println(name1 == name2);

Unexpectedly, the answer is 'true'.

In Java, Strings written by "" are set up only once if the content is the same, maintaining in String pool.

However, "new" is surely to set up a new object.

String name3 = new String("Justin");
String name4 = new String("Justin");
System.out.println(name3 == name4);

Its answer is 'false'. To compare two Strings with same content, use .equals().

Immutable String

String name2 = name1 + "World";

decompile:

String s1 = (new StringBuilder()).append(s).append("World").toString();

Text file encoding

Java supports Unicode.

Java API

I really really want to follow the steps in the book, BUT the network is too slow to open the page! Whatever, it's easy to search any API, if you can access high-speed Internet.

Chapter 5 Encapsulation

Encapsulation aims to hide details of objects. We use 'private' to keep members from outside. 'get+ObjectName' is a method to get members' value.

Members without declaration of jurisdiction can only be accessed in the same package. If you want to access it in another package's program, 'public' declaration is necessary.

Overload

class Other {
{
System.out.println("Initial");
} Other() {
System.out.println("Other() Constructor");
} Other(int o) {
this();
System.out.println("Other(int o) Constructor");
}
}

Tip : 'this()' can only appear in the first line of Constructor

'final' keyword

final int x;

There isn't '=', so x delays value appoint. But its constructor must appoint its value, otherwise compile fails.

'static'

Members declared with 'static' belong to class other than object.

class Ball {
double radius;
static final double PI = 3.1415926;
static double toRadians(double angdeg) {
return angdeg * (Ball.PI / 180);
}
} System.out.println(Ball.toRadians(100));

ClassName + '.' + static_members(or method)

Tip : static members belong to class, so it is a mistake to use 'this' in static members. Also, it can't include object's member, and non-static methods or blocks.

If you want to run some default activity, you can define static block:

class Ball {
static {
System.out.println("RUN");
}
}

import static

import java.lang.System.in;
import java.lang.System.in; .....
Scanner scanner = new Scanner(in);
out.print("...");

Tip : mind name confliction

Variable-length Argument

public class MathTool {
public static int sum(int... numbers) {
int sum = 0;
for (int number : numbers) {
sum += number;
}
return sum;
}
}

usage:

System.out.println(MathTool.sum(1, 2, 3));
System.out.println(MathTool.sum(1, 3));
System.out.println(MathTool.sum(1, 2, 3, 4));

Variable-length argument in method declaration:

  1. length parameter must be the last
  2. over one variable-length argument is illegal

教材学习中的问题和解决过程

这周的量比较大,主要就是边看边敲。不过都还是容易理解的。

其他(感悟、思考等,可选)

这次用英文感觉比上次流畅了些许。也不算难吧。

代码托管:

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 50/50 2/2 8/8
第二周 100/150 2/4 8/16
第三周 250/400 2/6 10/26

参考资料

20145109 《Java程序设计》第三周学习总结的更多相关文章

  1. Java程序设计第三周学习总结

    1. 本周学习总结 2. 书面作业 Q1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...

  2. 20145109《Java程序设计》第一周学习总结

    20145109 <Java程序设计>第一周学习总结 教材学习内容总结 About JVM, JRE, JDK JVM包含于JRE中,用于运行Java程序.JDK用于开发Java程序,包含 ...

  3. 对于“2017面向对象程序设计(Java)第三周学习总结”存在问题的反馈

    对于“2017面向对象程序设计(Java)第三周学习总结”存在问题的反馈 一:教学中存在的学习问题 “1.由于同学们平时练习不足,上课总是出现跟不上老师的节奏的现象. 2.个别同学上课不认真听讲,打开 ...

  4. 20145109《Java程序设计》第二周学习总结

    20145109 <Java程序设计>第二周学习总结 教材学习内容总结 Variable : naming rule : Camel case no default value e.g : ...

  5. 20145213《Java程序设计》第九周学习总结

    20145213<Java程序设计>第九周学习总结 教材学习总结 "五一"假期过得太快,就像龙卷风.没有一点点防备,就与Java博客撞个满怀.在这个普天同庆的节日里,根 ...

  6. 20145213《Java程序设计》第二周学习总结

    20145213<Java程序设计>第二周学习总结 教材学习内容总结 本周娄老师给的任务是学习教材的第三章--基础语法.其实我觉得还蛮轻松的,因为在翻开厚重的书本,一股熟悉的气息扑面而来, ...

  7. 21045308刘昊阳 《Java程序设计》第九周学习总结

    21045308刘昊阳 <Java程序设计>第九周学习总结 教材学习内容总结 第16章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 数据库本身是个独立运行的应用程序 撰 ...

  8. 20145330孙文馨 《Java程序设计》第一周学习总结

    20145330孙文馨 <Java程序设计>第一周学习总结 教材学习内容总结 刚开始拿到这么厚一本书说没有压力是不可能的,开始从头看觉得很陌生进入不了状态,就稍微会有一点焦虑的感觉.于是就 ...

  9. 20145337 《Java程序设计》第九周学习总结

    20145337 <Java程序设计>第九周学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC可以 ...

  10. 《Java程序设计》第九周学习总结

    20145224 <Java程序设计>第九周学习总结 第十六章 整合数据库 JDBC入门 ·数据库本身是个独立运行的应用程序 ·撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的 ...

随机推荐

  1. ChemDraw的“键”工具的作用是什么

    ChemDraw的“键”工具在化学研究中能够发挥重大作用,如果你也是一名化学相关工作者就千万不要错过这篇教程,下文详细盘点“键”工具究竟能干些什么. ChemDraw化学软件免费获取地址:http:/ ...

  2. 【转】MySQL常用命令总结

    http://blog.csdn.net/qq_33850438/article/details/52144686 MySQL常用的基本操作,以下都是MySQL 5.0下测试通过首先说明下,记住在每个 ...

  3. SurvivalShooter学习笔记(七.玩家射击)

    玩家射击:(这个脚本放在玩家的空子物体上,这个位置为枪口位置) 点击鼠标,玩家射击: 射击枪口发光,射击通过射线,方向有激光效果:(关于射线不明白可以参考Unity射线相关) 射击有射击音效 射击有每 ...

  4. servlet之模板方法和多线程

    接触了一小段时间的servlet,以下就总结一下关于servlet的两个方面的知识,一个是模板方法的应用.另外一个是servlet多线程产生的原因. 1. 模板方法设计模式 定义一个操作中的算法的骨架 ...

  5. Android中使用OnClickListener接口实现button点击的低级失误

    今天写了几行极为简单的代码,就是想implements  View.OnCLickListener.然后实现按钮点击操作.可是按钮却没有反应.找了五分钟还是没有结果. 下面是我的代码,希望大家不要嘲笑 ...

  6. mybatis的dao的注解

    import com.jianwu.domain.metting.model.CallPreMember;import com.jianwu.domain.metting.model.CallPreM ...

  7. js写css()方法,记得加引号“ ”,除非是数字

    js写css()方法,记得加引号“ ”,除非是数字.如: $("#android").css({ "position": "absolute" ...

  8. ios开源东西

    今天,我们将介绍20个在GitHub上非常受开发者欢迎的iOS开源项目,你准备好了吗? 1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项 ...

  9. Java日志记录工具SLF4J介绍

    SLF4J是什么 SLF4J是一个包装类,典型的facade模式的工具,对用户呈现统一的操作方式,兼容各种主流的日志记录框架,典型的有log4j/jdk logging/nop/simple/jaka ...

  10. 《深入理解Linux网络技术内幕》阅读笔记 --- 路由查找

    概述 1.不论是入口还是出口流量,都是利用fib_lookup来查找路由表,fib_lookup是对每一个路由表所提供的查找函数的包裹函数,当不支持策略路由时,查找函数版本针对的是local表和mai ...