Introduction to value types

13
Introduction to Value Types “Codes like a class, works like an int!” Narendran Solai 30-Jan-2016

Transcript of Introduction to value types

Page 1: Introduction to value types

Introduction to Value Types“Codes like a class, works like an int!”

Narendran Solai30-Jan-2016

Page 2: Introduction to value types

Disclaimer

• Value Types is work in progress• Likely to change• And I am not an expert.

Page 3: Introduction to value types

Is this too early to look at this?How Beneficial is it to peep into them and

contribute?

Page 4: Introduction to value types

Introduction to Project Valhalla

• Value Types is one of the feature under the project Valhalla

• Usually when new Language feature are introduced they connect with other existing feature or require more features.

• Source for Information -http://openjdk.java.net/projects/valhalla/

Page 5: Introduction to value types

Valhalla – From Mythology• In Norse

mythology, Valhalla (from Old Norse Valhöll "hall of the slain") is a majestic, enormous hall located in Asgard, ruled over by the god Odin.

• Thor, Loki and other Legendary heroes, lives in Asgard – a small planetary body.

Page 6: Introduction to value types

Before We get Into Details of Value Types, we shall see what are Value Objects? and How they

differ from Reference Objects?

Page 7: Introduction to value types

Value Objects vs Reference Objects• Do not confuse Value Objects with J2EE Transfer Objects.• All Objects in Java are Reference Object in General except for

primitive types (They are not considered as Object)• Defining Value Objects will provide the difference.• Value Objects

– Value Objects are Small Objects like Date & Money whose equality cannot be decided with Object Identity or Record Identity (from DB) rather only by Values.

– They can be passed around as Values instead of reference.– As for now Java does not provides first class Support for Value

Objects hence we need to override Equals & Hashcode Method and discard the use of Identity.

Page 8: Introduction to value types

Value Types – What is it?• A First Class Support for Value Object Creation.• New kind of type like classes, enum, interface etc.,• Small Value Objects, Immutable & Identity less• Combines the properties of Java’s current classes and primitive types.• Regarded as definitions of specially marked, specially restricted class definitions.• Simply a new kind of primitive type - “Codes like a class, works like an int!”Examples

– Identity less aggregates– User Defined Primitives– Immutable Records– Restricted Classes

Note: Value Types feature goes along with/hand in hand with Generic Specialization & Reified Generics and Enhanced Volatiles features.Despite Java pursues the requirement of Value types, it also take inspiration from C# & .Net First Class support for Value Types and not to repeat any drawbacks and to make it a fit for Java

Page 9: Introduction to value types

More Use Cases of Value TypesNone of the Below Requires Object Box.• Numerics – Complex Numbers • Native Types

– Mordern Processor Data Types• Algebraic Data Types

– Data types like Optional<T> or Choice<T,U>– Unit types (both void-like and metric)

• Tuples– multi-valued

• Cursors– An iterator or other cursor

• Flattening - natural way to express data structures with fewer pointer indirections.

Page 10: Introduction to value types

Changes & Impacts

• JVM Instruction Set• Java Language

Value Type

no call by value parameters in Java

no Array<int> in Java

functions return one – and only one – value at a time – No Tuples in Java

arrays are created in such a weird way, making them effectively scattered all around the memory

Page 11: Introduction to value types

Existing JVM Type System

• heterogeneous aggregates with identity (classes)

• homogeneous aggregates with identity (arrays)

• The only types that do not have identity are the eight hardwired primitive types: byte,short, int, long, float, double, char, and boolean.

Page 12: Introduction to value types

Moving Away from Object Type

• In Java 5, We got Wrapper Classes for Primitives.• Object identity has footprint and performance costs – Hence we have

primitives. These costs are most burdensome for small objects, which do not have many fields to amortize the extra costs.

• Object identity serves only to support mutability, where an object’s state can be mutated but remains the same intrinsic object.

• Programming idioms do not require identity, and would benefit from not paying the memory footprint, locality, and optimization penalties of identity.

• Moving away from state full objects with Identity to stateless object without Identity.

• Methods in Object.class like “wait” & “notify” are not required. And Synchronization is not required and should not allowed.