Core Java - Volume 1 Chapter 3

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

keyword: public

Access modifier; these modifiers control the level of access other parts of a program have to this code.

Initializing variables

After declaring a variable, you must explicitly initialize it with an assignment statement - you can never use the value of an uninitialized variable. You can put declarations anywhere in your code. You assign to a previously declared variable by using the variable name on the left, an equal sign (=), and then some Java expression with an appropriate value on the right. Ex: int vacationDays; vacationDays = 12; You can declare and initialize a variable on the same line. Ex: int vacationDays = 12;

Relational and boolean operators

== tests for equality. Ex: 3 == 7 is false. != tests for inequality. Ex. 3 != 7 is true. < (less than) > (greater than) <= (less than or equal ) >= (greater than or equal) && for the logical "and" operator || for the logical "or" operator

Enumerated types

An enumerated type has a finite number of named values, Ex: enum Size { SMALL, MEDIUM, LARGE }; Size s = Size.MEDIUM; In this example a variable of type Size can hold only one of the values listed in the type declaration, or the special value null that indicates that the variable is not set to any value at all.

Class constant

Class constants, which are declared using static final, create a constant so it's available to multiple methods inside a single class.

Strings

Conceptually, Java strings are sequences of Unicode characters. Java does not have a built-in string type, instead the standard Java library contains a predefined class called String. Each quoted string is an instance of the String class: String e = ""; // an empty string String greeting = "Hello";

Substrings

Extract a substring from a larger string with the substring method of the String class. Ex: String greeting = "Hello"; String s = greeting.substring(0, 3); creates a string consisting of the characters "Hel". The second parameter of substring is the first position that you do not want to copy.

Floating-point types

Floating-point types denote numbers with fractional points. There are two floating-point types: 1. float: 4 bytes, 6-7 significant decimal points 2. double: 8 bytes, 15 significant decimal points The name double refers to the fact that these numbers have twice the precision of the float type. Double is the most commonly used. Floating-point numbers are not suitable for financial calculations in which rounding errors cannot be tolerated. Use the BigDecimal class if precise numerical calculations without rounding errors are needed.

keyword: class

Following the keyword class is the name of the class. Class names must begin with a letter, after which they can have any combination of letters and digits. Cannot use a Java reserved word. CamelCase is standard naming convention.

Variables

In Java, every variable has a type. You declare a variable by placing the type first, followed by the name of the variable, followed by a semicolon (because a declaration is a complete Java statement). Variables must begin with a letter and must be a sequence of letters or digits (defined as any Unicode character that denotes a letter or digit in a language).

Constants

In Java, you use the keyword final to denote a constant; the keyword final indicates that you can assign to the variable once, and then its value is set once and for all. Customary to name constants in all uppercase.

keyword: void

Indicates that this method does not return a value.

Integer types

Integer types are for numbers without fractional parts. Negative values are allowed. There are four integer types: 1. int: 4 bytes, -2.1 billion to 2.1 billion 2. short: 2 bytes, -32.7k to 32.7k 3. long: 8 bytes, -9.2 quintillion to 9.2 quintillion 4. byte: 1 byte, -128 to 127 The sizes of all numeric types are platform-independent, and there are no unsigned types. Int is most commonly used.

Compile

Java compiler compiles source code into bytecode file, and automatically names the bytecode file with the same name as the source code but with a .class extension, and stores it in the same directory as the source file.

Increment and decrement operators

Java has both increment and decrement operators: n++ adds 1 to the current value of the variable n, and n-- subtracts 1 from it. These operators cannot be applied to numbers themselves, for example, 4++ is not a legal statement.

Raising to a power

Java has no operator for raising a quantity to a power: you must use the pow method in the Math class. double y = Math.pow(x, a);

Comments

Java has three ways of marking comments. 1. // used for a comment that will run from the // to the end of the line. 2. /* and */ comment delimiters let you block off a longer comment. 3. /** and */ can be used to generate documentation automatically.

statement

Java statements can be thought of as sentences of the language; every statement must end with a semicolon. Carriage returns do not mark the end of a statement, so statements can span multiple lines if need be.

Method

Java-speak for a function. Braces mark the beginning and end of the body of the method.

Parameter

Methods can use zero, one, or more parameters. Parameters are the variables that are listed as part of a method declaration. Each parameter must have a unique name and a defined data type. If a method takes no parameters, you must still use empty parentheses.

Braces { }

Braces delineate the parts (usually called blocks) in your program. In Java, the code for any method must be started by an opening brace { and ended by a closing brace }.

Describe the following code: { System.out.println("We will not use 'Hello, World!'"); }

Braces mark the beginning and end of the body of the method. This method has only one statement. System.out is an object, and println is its method being called. ("We will not use 'Hello, World!'") is a string parameter being passed to the println method.

Main method

Required in the source file for your class for your code to execute. The Java virtual machine always starts execution with the code in the main method in the class you indicate. You can add your own methods to a class and call them from the main method. The main method must be declared public, and is always static.

1_000_000

Starting with Java 7, you add underscores to number literals, such as 1_000_000 to denote one million. The underscores are for human eyes only; the Java compiler simply removes them.

System.out.println

System.out is the object, and println is its method.

boolean type

The boolean type has two values, false and true, used for evaluating logical conditions. You cannot convert between integers and boolean values.

char type

The char type is used to describe individual characters; these will most commonly be character constants.

Source code file name

The file name for the source code must be the same as the name of the public class, with the extension .java appended.

T or F: Java is a strongly typed language.

True. A strongly typed language means that every variable must have a declared type, of which there are eight primitive types.

Concatenation

Use a + to join (concatenate) two strings. When you concatenate a string with a value that is not a string, the latter is converted to a string. Every Java object can be converted to a string.

Escape sequences

\b backspace \t tab \n linefeed \r carriage return \" double quote \' single quote \\ backslash \u indicates encoding of Unicode code units; only escape sequence that can be used outside quoted characters (for example: public static void main(String\u005B\u005D args). The \u005B and \u005D are the encodings for [ and ].

General syntax to invoke a method

object.method(parameters)


Ensembles d'études connexes

Saludos y expresiones de cortesía

View Set

Intro to Entrepreneurship: Chapter 3

View Set

PMI Agile Certified Practitioner (PMI-ACP)

View Set

Health Test Chapter 7, 8, and 9 Review Mr. Sloan

View Set