Java 8 Programming I - Chapter 3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

short

16 bits, -32768 to 32767 Probably the least used type.

int

32 bits, -2,147,483,648 to 2,147,483,647 Most commonly used integer type. byte and short values are promoted to int when expressions are evaluated.

A few words about Strings

A few words

Variable Declarations

Any combination of letters (capital or lower case), numbers, underscore or dollar sign. May not begin with a number. Case-sensitive. type identifier [ = value] [,identifier [ = value] ... ];

Arrays

Group of like-typed variables that are referred to by a common name.

Floating-Point Types

Sometimes known as 'real' numbers. IEEE-754 Standard

One-dimensional arrays

type var-name[]; type var-name[] = new type[size]; type var-name[] = { n1, n2, n3, ... , nn }; Automatically set to * "0" for numeric types * "false" for Boolean types * "null" for reference types

Casting Incompatible Types

"narrowing conversions" are accomplished through a "cast": (target-type) value; The "target-type" is the desired type to convert the specified value to. For same-type casts, the value is reduced modulo of the target-type size. If a floating-point type is converted to integer, the fractional component is truncated.

Other "var" Restrictions

"var" requires an initializer (so it can inference the type) var counter; // Error "var" cannot be used as the name of a class. Only one variable can be declared at a time. Cannot use "null" as an initializer. Cannot be used as the name of other reference types: * Interface * Enumeration * Annotation * Name of a Generic Type Parameter Cannot use "var" with an array initializer: var myArray = { 1, 2, 3 }; // Error Cannot be used to declar the exception type caught by a "catch" statement. Neither lambda expressions nor methods can be used as initializers

"var"

(JDK 10) The context-sensitive identifier "var was added as a reserved type name for this. "Context-Sensitive" // This creates an integer variable "var" with a value of 1 int var = 1; // This creates a double variable with a value of 10.0 var avg = 10.0;

"var" in Arrays

(JDK 10) To use "var" in an array: var myArray = new int[10]; // This is valid Cannot use brackets on the left side of the declaration: var[] myArray = new int[10]; // Error var myArray[] = new int[10]; // Error

Type Inference with Local Variables

(JDK 10) Variable Type Inference - the compiler will infer the type o fa local variable based on the type of is initializer. * Steamline code by eliminating the need to redundantly specify a varialbe's type when it can be inferred * Simplifiy declarations in cases in which the type name is quite lengthy * When the type is difficult to discern or cannot be denoted (e.g., anonymous classes)

Type Promotion Rules for Expressions

1) All byte, short and char are converted to int 2) If any one operand is long, the whole expression is promoted to long. 3) if any one operand is float... 4) if any one operand is double...

float

32-bits, 1.4e-45 to 3.4e38 "single-precision" Useful when you need a fractional component, but don't require a large degree of precision.

long

64-bits, -9 x 10^18 to 9 x 10^18

double

64-bits, 4.9e-324 to 1.8e308 "double-precision" Actually faster than single-precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin(), cos() and sqrt() return double values. Used to maintain accuracy over may iterative calculations, or manipulating large-valued numbers.

byte

8-bits, -128 to 127 Useful when streaming data from a network or file, or when working with raw binary data that may no be directly compatible with other types

Variable Dynamic Initialization

Allows variables to be initialized dynamically, using any expression valid at the time the variable is declared.

Character Literals

Can be represented by a single pair of quotes (e.g., char c = '@';). Can use '\' to assign an escape sequence. Can use the Unicode character in single quotes '\u0061'

Floating-Point literals

Can be used to express Standard or Scientific notation. Standard notation consists of a whole number component followed by a decimal point and a fractional component. Scientific notation consists of standard notation, floating point number plus a suffix that specifies a power of 10 y which the number is multiplied. The exponent is indicated by a "E" or "e" following the decimal number (e.g., 6.022e23) Default to double precision. To specify a float literal, an "f" or "F" needs to be appended. (e.g., 1.01f) Double precision can be explicitly specified by appending a "d" or "D". (e.g., 1.01d) Hexadecimal floating points are also supported by appending using "p" or "P" instead of "e" or "E". (e.g., 0x12.2P2, which evaluates to 72.5) "P" designates the power of 2 the value is multiplied by. 0x12 = 18 0x0.2 = 2 * 1/16 = 1/8 = 0.125 18.125 * 2^2 = 72.5. Can embed underscores in-between digits but not at the beginning or end.

Variable Scope and Lifetime.

Defined by the block in which they're defined. A block defines a "scope" Defined by "class" and "method" Class scope is discussed in Chapter 6 Method scope ends with its closing curly brace. If the method has parameters, they are included in the method's scope. The block of code in the method is called the "method body". Variables are destroyed when their scope is exited. Scopes can be nested, but you cannot repeat variable names in child scopes.

Automatic Type Promotion in Expressions

In expressions, byte, short and char are converted to int. Must explicitly cast back to the original type and handled accordingly: byte b = 50; // b = b * 2; (Error: cannot assign a int to a byte) b = (byte)(b * 2);

Integer Literals

Octal numbers are denoted by a leading zero. (e.g., 07) Hex numbers are denoted by a leading "0X" or "0x". (e.g., 0xB4B3) Binary values are denoted by a leading "0B" or "0b" (e.g., 0b1101) Integer Literals create an "int" value. Integer Literals assigned to byte or short types do not cause an error if the literal fits within the type's range (narrowing conversion). To specify long, an appended "L" is used. Underscores can be embedded between digits to help readability. They cannot be used at the beginning or end of the String.

strongly typed programming language

Programming language that requires you to declare the data types of variables. All assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility. There are no automatic coercions or conversions of conflicting types.

Boolean

Special type for representing true and false. These are non-numeric values.

A few more words about strings

Strings are immutable objects and are not a primitive type.

String Literals

Surrounded by double-quotes. Must begin and end on the same line. There is no line-continuous sequence in Java.

char

Used to store characters. Note: chars are 16-bits, unlike in C or C++ where they are 8-bits wide. Java uses Unicode to represent characters. There are no negative characters. 0-65,535 ASCII is 0-127 ISO-Latin-1 is 0-255 Printing a character value using "println()" results in the text character of the numeric value to be shown. Can be used as an integer type, but is not promoted to integer during evaluation.

Alternative Array Declaration Syntax

Useful when specifying an array as the return type of a method. type[] var-name = new type[size]; type[][] var-name = new type[size1][size2];

Automatic Type Conversion

When one type of of data is assigned to another type of variable, automatic type conversion will occur when the following two conditions are met: 1) The two types are compatible 2) The destination type is larger than the source type (widening conversion). No automatic conversion to char or boolean, and char and boolean are not compatible with each other.

Escape Sequences

\ddd - Octal \uxxx - Hexadecimal Unicode Character \' - Single Quote \" Double Quote \\ Backslash \r Carriage Return \n New line / line feed \f Form Feed \t Tab \b Backspace

Integers

byte, short, int and long which are whole-valued signed numbers. width of an integer type should not bethought of as the amount of storage it consumes, but rather as the behavior it defines for variables and expressions of that type.

Primitive Types

byte, short, int, long, char, float and double. For the basis for all other types you can create. These are not complex objects, and therefore perform better than complex objects. Have an explicit range and mathematical behavior.

Characters

char - represents symbols in a character set, like letters and numbers

Floating-point numbers

float and double, which represent numbers with fractional precision

Boolean Literals

true or false and do not convert to any numeric representation

Multi-dimensional arrays

type var-name[][] = new type[size1][size2]; type var-name[][] = { { n11, n12}, {,n21, n22}}; Only the left most array need be declared as you dive into the multi-dimensional array. This allows different size values as the arrays to the right are declared. (uneven, irregular, jagged arrays)


Kaugnay na mga set ng pag-aaral

Chapter 5: Evaluating Research Review

View Set

Sir Gawain and the Green Knight Parts 3 Study Guide

View Set

Chem152: Chapter 6, Thermodynamics (& Chapter 9.10 Bond Energies and Lengths)

View Set

history final multiple choice and t/f

View Set

Chapter 28: Nursing Management: Patients With Urinary Disorders

View Set