Java Fundamentals

Ace your homework & exams now with Quizwiz!

Switch Example

'char control = 'a'; switch (control) { case 'a': invokeA(); break; default: invokeError(); break; }' Note that because 'a' is logically equivalent to the switch argument 'control', that case would be selected and, once the statements executed, break would transfer control to the end of the switch statement, skipping the default: case.

Prefix/Postfix Operators

++value = adds one then does the statement value++ = does the statement then adds one - int num = 9; int x = num --; num = x++; x= num ++;

Exiting from a Method

A method exits for one of three reasons -The end of the method is reached -A return statement is encountered -An error occurs Unless there's an error, control returns to the method caller

Boolean Type

A type with two possible values: true and false.

Block statements and Variable Scope

A variable declared within a block is not visible outside the block A variable's range of visibility is known as the variable's scope Variable's within a block when a block starts remain in scope variables created after are not

Package names

Affect the source code file structure. Your source file structure needs to match your file structure to be compatible with most IDEs.

If-else statement

An if statement conditionally executes a statement if (condition) true-statement; The optional else clause executes a statement when the if condition is false

Conditional Assignment

Assign a value to a variable based on the result or condition

strongly typed language

Assigned values must correspond to the declared type. The value can be modified in a variable in Java

Variables

Can be declared anywhere you want to within your code structure

Explicit type conversions

Can perform widening and narrowing conversions Floating point to integer drops fraction Use caution with narrowing conversions Integer to floating point can lose precision

Compound assignment operator

Combines an operation and assignment Applies result of right side to left side Stores that result in variable on left side Available for 5 basic math operators +=, -=, *=, /=, %=

Type Conversions

Compiler can automatically apply widening type conversions Use type casting to explicitly perform type conversions

Implicit type conversion

Conversions performed automatically by the compiler

Explicit type conversion

Conversions performed explicitly in code with cast operator

Using classes

Declare a variable using the keyword -Allocates memory -Returns a reference to the allocated memory

Primitive Types stored by value

Each value is assigned its own memory space

Method Basics

Executable code that manipulates state and performs operations - Name - Same rules and conventions as variables - Should be a verb or action - Return type - Use void when no value returned - Type parameter list - Can be Empty - Body contained with brackets

for each loop (enhanced for loop)

Executes a statement once for each member in an array -Handles getting collection length -Handles accessing each value for( loop-variable-declaration:array ) statement;

Applying access modifiers to classes.

For the OCA exam you only need to know that classes are public when you use the public keyword. If you omit this keyword, only subclasses and classes from the same package can access the class. Remember you can have multiple classes or interfaces in your file, as long as only one of them is public.

Integer Data Type

Four types: byte = 8 byte Min Value (-128) Max value (127) Literal format 0 short = 16 byte Min Value (-32768) Max value (32767) Literal format 0 int = 32 byte Min value (-2147483648) Max value (2147483647) Literal format 0 long = 64 byte Min value (-9223372036854775808) Max value (9223372036854775807) Literal format 0L (after a long integet type integer value must end with an 'L'

Public Class

Has to be in a public file by the same name

Chaining if-else statements

If-else statements chained together are evaluated in order until one is true

Arrays

Important part of Java Allow you to store multiple values under a single name Provides an ordered collection of elements -Each element accessed via an index -Indexes range from 0 to number-of-elements minus 1 -Number of elements can be found via array's length value

Encapsulation

Keeping details (like data and procedures) together in one part of a program so that programmers working on other parts of the program don't need to know about them.

Project name

Last part of package name example com.pluralsight.getorganized

Classes in Java

Made up both state and executable code -Fields -Store object state -Methods -Executable code that manipulates state and performs operations -Constructors -Executable code used during object creation to set the initial state

Packages

Naming starts with domain name with 'com' first then project then class. Example. com.pluralsight.accounting.main or com.pluralsight.crsemngmt.main

Block statements

One or more statements enclosed in an open curly brace '{' and a closing curly brace '}'. Independent from one another

Operators precedence

Operators are evaluated in a well-defined order Operators of equal precedence are evaluated left-to-right Can override precedence with parenthesis Nested parentheses evaluated from the inside out

Naming Classes

Pascal Case example "BankAccount" the first letter is capitalized

Do-While Loop

Repeatedly executes a statement as long as the condition is true -Condition checked at loop end -Statement always executes at least once

While Loop

Repeatedly executes a statement as long as the condition is true -Condition checked at loop start -Statement may never execute

For Loop

Repeats executes a statement as long as the condition is true -Condition checked at loop start -Provides simplified notation for loop control values for (initialize; condition; update) statement;

Conditional Logical Operators

Resolve following conceptually similar rules as non-conditional and/or Only execute the right-side if needed to determine the result -&& only executes right-side if left-side is true --| | only executes right-side if left-side is false

Relatonal Operators

Tests values against each other

Character (char) Type

The char type stores a single Unicode character - Literal values placed between single quotes - For Unicode code points, use \u followed by 4-digit hex value

Floating Point Types

Those that contain fractional parts using significand digits and an exponent which represents its magnitude. Includes floats and doubles (32 and 64 bits respectively).

Loops

Three basic ways to write loops in Java: While Loop Do-while loop For loop

Switch

Transfers control to a statement based on a value -simplifies testing against multiple possible matches -Only primitive types supported are char and integers -A match can execute more than one statement -Use break to avoid "falling through" -Can optionally include default to handle my unmatched values switch(test-value) { case value-1: statements case value-2: statements . . . case value-n: statements default: statements }

Method Return Value

Value Returned by method Back to Calling method

Implicit Type Conversion

Widening conversions are automatic Mixed integer sizes -Uses largest integer in equation Mixed floating point sizes -Uses double Mixed integer and floating point -Uses largest floating point in equation

Primitive Data Types

byte, short, int, long, float, double, char, boolean Foundation of all other types

Arithmetic Operators

perform mathematical calculations (Add +, Subtract -, Multiply *, Divide /, Modulus %


Related study sets

Chapter 1 Smart Book Managerial accounting

View Set

Medication Administration: Intramuscular Injection Elsevier Military

View Set

Ch. 13 - Monopolistic Competition

View Set

Gastrointestinal Disorders NCLEX 3000

View Set

[MKT 201] Chapter 9 - Developing New Products and Managing the Product Life Cycle

View Set

Foundations exam 1-4 missed by mercedes

View Set

The Complete Skinny on Obesity Worksheet

View Set