栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Numbers

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Numbers

final: Variables, Methods and Classes

Sometimes we don’t want child classes to override the implementation in the parent!

Java keyword final. – Using this keyword will prevent child classes (or anyone else) modifying the variable/method this applies to.We can prevent a class from being inherited at all!

Suppose we want to keep track of how many instances of a class are made. – Question: How can we automatically keep track? • cannot leave it to the objects; • cannot use an instance variable. – Answer: Use a class variable. • A class variable is created when the class is created, rather than when an object is created. – There may be n instances of the class, but there will only be one instance of the class variable. – To declare a class variable, use the modifier static.

Normally every instance of an object has its own copy of all variables and methods defined in the class.If you declare something as static, it means that all objects have the same copy of that variable/method.

Static methods can only reference static variables!

Static is like global variables but applies classwide. • Static methods become available when the class is loaded (created), not when you make an instance of it

Can access static methods even when no instance of a class has been created!

Math Class and Methods

Methods of class Math: almost like global methods! – Act on the argument but are not affected by an instance variable state.

Example: int x = Math.min(56,12); always does the same thing and doesn’t use instance variables.  Method’s behaviour doesn’t need to know about a given object.

Math Class: – Doesn’t have instance variables. – Can’t make an instance of class.

For static method ,it is no sense to use them as instance method

Classes that can’t be instantiated: – Abstract classes and interfaces. – Classes with private constructors. • Only code inside the class can invoke a private constructor! • Methods in class Math are static.

– Invoking a Math method:Math.max(10,20);//calling a static method: use the class name

A non-initialised static variable will have the default value of that variable type.

naming convention for static final variables: all caps

static block to initialise static final variables

final class: cannot extend the class

final classes and abstract classes: not the same!

Math.random(): returns a double in range 0.0-1.0 (excluding).

• Math.abs(double num): returns a double that is the absolute value of num; method is overloaded to take/return an int value.

• Math.round(float value): returns a value rounded to the nearest int value; method is overloaded to take a double value and return a long value.

• Math.min(int a, int b): returns an int that is the minimum value between a and b; method is overloaded to take/return a long, float or double value.

• Math.max(int a, int b): returns an int that is the maximum value between a and b; method is overloaded to take/return a long, float or double value.

Wrapping Classes: used when a variable of a primitive type needs to be treated as an object. – Every primitive type has a wrapper class. – Wrapper classes are part of java.lang package  no need to import them

Autoboxing: automatic wrapping  conversion from primitive type to wrapper object is automatic!

Integer i = new Integer(10); i++; System.out.println(“New value is: “ + i);

Integer d = x;int x = 10;Integer x = 10;

Wrappers have parse methods: they take a String and return a primitive value.

String str1 = “10”; String str2 = “123.45”; String str3 = “true”; int i = Integer.parseInt(str1); // i=10 double d = Double.parseDouble(str2); // d=123.45 boolean b = new Boolean(str3).booleanValue(); // b=true

All wrapper classes are subclasses of the Number abstract class (part of the java.lang package).

Number num = new Integer(10);

Subclasses of Number provide constants to, e.g. represent the upper and lower bounds of the corresponding data type (MIN_VALUE and MAX_VALUE, respectively). • Example: System.out.println(Long.MIN_VALUE);  –263

Methods that call themselves, directly or indirectly, are known as recursive

Extra memory required + time to manage the extra space.

• use an iterative approach if that is the obvious solution; • avoid using recursion if there are concerns about the program’s performance.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/894734.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号