C # Programmer's Scala path Nineth (Scala's hierarchy)

Source: Internet
Author: User

Class level for 1.Scala

The top of the class in Scala is any class that inherits any classes, and any includes the following common methods:

Final def == =(That:any): Boolean def equals (That:any): Boolean def hashcode:int def tostring:string

2 operator overloads, one Equals method, one Hashcode method and one tostring method.

Root class any has two sub-classes: Anyval and Anyref

Anyval is the parent of a value type, and Anyref is the parent class of the reference type.

Here's the Scala hierarchy:

The value class inherits all of his methods from any:

println (printlnhashcode) println (42.equals (42))

When needed, the class Scala. An instance of int can be automatically relaxed (by implicit conversion) to an instance of the class Scala.long.

Implicit conversions are also used to add more functionality to value types. For example, type int supports all of the following actions:

println (Max) println (1 until 5) println (1 to 5) println(3. Abs) println ((-3). Abs

How did all this come true? In fact, Scala stores integers in the same way as Java: use it as a 32-bit word.
This is important for efficiency on the JVM and interoperability with the Java library.
Standard operations, such as addition or multiplication, are implemented as primitive operations.
However, when integers need to be viewed as (Java) objects, Scala uses the "Backup" class Java.lang.Integer.
This is done if you call the ToString method on an integer or assign an integer to a variable of any type. When needed, integers of type int can be transparently converted to a boxed integer of type Java.lang.Integer.
All of this sounds like auto-boxing in Java5 and they really look alike.
There is a key difference, however, that the boxed box in Scala is less visible than in Java. Try the following Java code:

boolean isequal (int x,int  y) {     return x = = y;} System.out.println (isequal (421,421//boolean  isequal (Integer x, Integery) {     return x = = y;} System.out.println (IsEqual (421,421));

You'll find you got false!.
The reason is that the number 421 is boxed two times, so the parameters x and Y are two different objects.
because = = on a reference type means reference equality, and integer is a reference type, the result is false.
This is an aspect of showing that Java is not a purely object-oriented language. We can clearly observe the difference between the original type and the reference type.
Now try the same experiment in Scala:

extends App {  = x = = y  println (isequal)}

In fact, the equivalent operation in Scala = = is designed as a transparent reference type to represent something.
For a value type, it is natural (mathematical or Boolean) equal.
For reference types, = = is considered an alias of the Equals method that inherits from object.
This method is initially defined as reference equality, but is overloaded by many subclasses to implement the concept of equality for their races.
This also means that in Scala you will never fall into Java's well-known traps for string comparisons.

。 In Scala, a string comparison works the way it should:

  New String ("abc")  new string ("abc")  = = y  )/*  */   println (x eq y)  /* Compare references unequal   /* println (x ne y) 

3 Bottom Type

At the bottom of the emoticon type hierarchy you see two classes of Scala. Null and scala.nothing. They are a special type of "boundary condition" that handles certain Scala's object-oriented type systems in a uniform manner.

The class null is a reference to a null type, which is a subclass of each reference class (that is, each class that inherits from Anyref). Null incompatible value type. You can't, let's say, assign a null value to an integer variable.

The type nothing is at the bottom of the Scala class hierarchy; it is any other type of subtype.
However, there is no value of this type at all.
What does it mean to have a type that has no value? As discussed in section 7.4, one of the uses of nothing is that it indicates an abnormal termination.
For example, the Predef object in Scala's standard library has an error method, as defined below:

Throw New RuntimeException (Message)

The return type of error is nothing, which tells the user that the method is not returning normally (instead of throwing an exception). Because nothing
Is any other type of subclass, you can use a method like error very flexibly. For example:

if Else

"Then" the state branch, X/y, type int, and "otherwise" branch, the error is called, and the type is nothing.
Because nothing is a subtype of int, the entire state statement is of type int, as needed.

C # Programmer's Scala path Nineth (Scala's hierarchy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.