Fundamentals of Java

¡Supera tus tareas y exámenes ahora con Quizwiz!

Primitive Data Type - *char*

- *char* is short for character and can represent a single character. - *All char values must be enclosed in single quotes, like this: 'G'.*

Comments

- A *comment* is text you want Java to ignore. Comments allow you to describe code or keep notes.

Primitive Data Types

- A *primitive* data type is predefined by the language and is named by a reserved keyword. - Primitive values do not share state with other primitive values. This means that if a variable *i* of type *int* is declared and given an initial value of *0*, then another *int* variable *j* is declared and assigned the value of *i*, then both variables would have the value *0*. If *i* is later assigned a different value, such as *1*, the value of *j* is not affected - *j* would still have a value of *0*. See the code below: int i = 0 ; int j = i ; i = 1 ; System.out.println( j ); // prints 0

Variables

- A variable stores a value. - *In Java, all variables must have a specified data type.*

Equality Operators

- In Java, equality operators are used to test equality. - The equality operators are: *==* equal to. *!=* not equal to. - Equality operators do not require that operands share the same ordering. You can test equality across *boolean*, *char*, or *int* data types.

Multi-line Comments

- Multi-line comments are comments that can span multiple lines. They are enclosed in symbols containing forward slashes and asterisks. */** Hello, Java! **/*

Relational Operators

- Relational operators compare data types that have a *defined ordering*, such as numbers. - Relational operators will *always* return a boolean value of true or false. - Here are a few relational operators: *<* less than. *<=* less than or equal to. *>* greater than. *>=* greater than or equal to.

Logical Complement Operator

- Represented by: *!* - Inverts the value of a boolean. (True becomes false, and vice versa)

Unary Plus Operator

- Represented by: *+* - Indicates positive value of the operand *(numbers are positive by default without this, however)*

Increment Operator

- Represented by: *++* - Increments a value by 1.

Unary Minus Operator

- Represented by: *-* - Negates an expression

Decrement Operator

- Represented by: *--* - Decrements a value by 1.

Primitive Data Type - *int*

- Short for *integer*, which are all positive and negative numbers, including zero. - The *int* data type only allows values between -2,147,483,648 and 2,147,483,647.

Single Line Comments

- Single line comments are one line comments that begin with two forward slashes. - Example: *//* I'm a single line comment!

Primitive Data Type - *boolean*

- The *boolean* data type has only two possible values: true and false. - Use this data type for simple flags that track true/false conditions. - This data type represents one bit of information, but its "size" isn't something that's precisely defined.

Primitive Data Type - *byte*

- The *byte* data type is an integer with a minimum value of -128 and a maximum value of 127. - The *byte* data type can be useful for saving memory in large arrays, where the memory savings actually matters. - They can also be used in place of *int*, where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

Primitive Data Type - *double*

- The *double* data type is a *double-precision* floating point number. - For decimal values, this data type is generally the default choice. - *This data type should never be used for precise values, such as currency.*

Primitive Data Type - *float*

- The *float* data type is a *single-precision* floating point number. - As with the recommendations for *byte* and *short*, use a *float* (instead of *double*) if you need to save memory in large arrays of floating point numbers. - *This data type should never be used for precise values, such as currency.*

Primitive Data Type - *long*

- The *long* data type is an *integer* with a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807. - Use this data type when you need a range of values wider than those provided by *int*.

Primitive Data Type - *short*

- The *short* data type is an *integer* with a minimum value of -32,768 and a maximum value of 32,767. - As with *byte*, the same guidelines apply: you can use a *short* to save memory in large arrays, in situations where the memory savings actually matters.

Unary Operators

- The unary operators require only *one operand*. They perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

Variable Assignment

- We can assign a variable to a specified data type using the *assignment operator (=)* - It assigns the value on its right to the *operand* on its left. - Ex: int myLuckyNumber = 7; - A *semicolon* (*;*) is used to end all Java single code statements.

Arithmetic

In Java, you can add, subtract, multiply, and divide numbers using these operators: Addition: *+* (also used for string concatenation) Subtraction: *-* Multiplication: * * * Division: */*

Modulo Operator

The *modulo* operator - represented in Java by the *%* symbol - returns the remainder of dividing two numbers.

Java Variable Declaration

The Java programming language is *statically-typed*, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name. Example: int gear = 1;


Conjuntos de estudio relacionados

Biology - The Chemical Basis of Life

View Set

Angles of Elevation and Depression Math

View Set

Quiz Bowl Cert - Countries once known by a different name

View Set