Chapter 2 Java Fundamentals

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Primitive Data Type Ranking

* Highest Rank * double I float I long I int I short I byte v * Lowest Rank * Ex.) A short can be stored in a int but a double cannot be stored in an int.

Boolean Data Type

- Boolean variables may hold only the value true or false - The contents of a boolean variable may not be copied to a variable of any type other than boolean.

Special Characters

// Double slash marks the beginning of a comment ( ) Open and close parenthesis used in a method header { } Open and close braces encloses a group of statements, such as the contents of a class or a method. " " Quotation marks enclose a string of characters, such as a message that is to be printed on the screen ; Semicolon marks end of complete programming statement

Literal

A literal is a value that is written into the code of a program. Literals are commonly assigned to variables or displayed. * Examples * Code Listing 2-9 20 - Integer Literal "Today we sold" - String Literal

Reference Variables

A reference variable does not hold the actual data item that is associated with, but holds the memory address of the data item it is associated with. For example, if name is a String class variable, then name can hold the memory address of a string object.

Application Programmer Interface (API)

A standard library of prewritten classes for performing specific operations. These classes and their methods are available to all Java programs.

Variable Declaration

A variable declaration tells the compiler the variable's name and the type of data it will hold.

Variable

A variable is a named storage location in the computer's memory. Variables allow you to store and work with data.

Named Constant

A variable whose value is read only and cannot be changed during the program's execution. You can create such variable using the final key word. The word final is written before the data type. * Example * final double INTEREST_RATE = 0.069; doesnt have to be in all caps but helps distinguish from other variables.

Scope

A variable's scope is the part of the program where the variable may be accessed by its name. A variable is visible only to statements inside the variable's scope.

Escape Sequences

An escape sequence starts with the backslash character (\) and is followed by one or more control characters. It allows you to control the way output is displayed by embedding commands within string itself. \n Newline advances the cursor to the next line for subsequent printing. \t Horizontal tab causes the cursor to skip over to the next tab stop \b Backspace causes the cursor to back up, or move left, one position \r Return causes the cursor to go to the beginning of the current line, not the next line \\ Causes a backslash to be printed \' Single Qoute causes a single qoutation to print \" Double Qoute causes a double qoutation to print

Identifiers

An identifier is a programmer-defined name that represents some element of a program. Variable names and class names are examples of identifiers. Name variables to where if someone reads your code they know exactle what everything is and can follow it and know what it does. For example instead of naming an integer x name it itemsOrdered. by capitalizing O it's easier to read and they know that that value is the number of items ordered. *Look at Table 2-4 page 43 in book.*

A String Object

Any time you write a string literal in your program, Java will create a String object in memory to hold it. - What is a literal? Synonym to literal is constant. It's literally what you see it's not going to change. It's inside quotes. Versus a variable can be changed. Example: String name = "Joe Mahoney";

Conversion Between Primitive Data Types

Before a value can be stored in a variable, the value's data type must be compatible with the variable's data type. Java performs some conversions between data types automatically, but does not automatically perform any conversion that can result in the loss of data. Java also follows a set of rules when evaluating arithmetic expressions containing mixed data types.

Combined Assignment Operators

Combine the assignment operator with the arithmetic operators. May look a little confusing because the same variable name appears on both sides of the assignment operator. * Example * x = x + 4; balance = balance - withdrawal; * Could be re-written as * x += 4; balance -= withdrawal;

Comments

Comments are notes of explanation that document lines or sections of a program. Comments are part of the program, but the compiler ignores them. They are intended for people who may be reading the source code. (//) Single Line Comments (/*) Multi-line Comments(*/) (javadoc) creates HTML files that document the source code. Start with (/**) & end with (*/)

Primitive Data Types

Data Type/Size/Range 1) byte/ 1 byte/ -128 to 127 2) short/ 2 bytes/ -32,768 to 32,767 3) int/ 4 bytes/ -2,147,483,648 to 2,147,483,647 4) long/ 8 bytes/ Biggest numbers EVER (-)to(+) 5) float/ 4bytes/ (+/-) scientific notation numbers 6) double/ 8 bytes/ decimal numbers

Primitive Type Variables

Hold the actual data items with which they are associated. For example, assume that number is an int variable. The following statement stores the value 25 in the variable: number = 25;

The Math Class

Math.pow( ) Method Math.sqrt( ) Method Math.abs( ) Method

Reading Keyboard Input

Objects of the Scanner class can be used to read input from the keyboard. import java.util.Scanner

Programming Style

Programming style refers to the way a programmer uses spaces, indentations, blank lines, and punctuation characters to visually arrange a program's source code. PS usually refers to the way the source code is visually arranged.

String Concatenation Operator

The (+) operator is known as the STO. The concatenate means to append, so the string concatenation operator appends one string to another. You can also use the (+) operator to concatenate the contents of variables to a string. * Example * System.out.println("This is " + "one string."); - The system will print: This is one string. number = 5; System.out.println("The value is " + number); - The system will print: The value is 5

Creating Named Constants with Final

The /final/ key word can be used in a variable declaration to make the variable a named constant. Named constants are initialized with a value, and that value cannot change during the execution of the program.

JOptionPane

The JOptionPane class allows you to quickly display a dialog box, which is a small graphical window displaying a message or requesting input. import javax.swing.JOptionPane; * Displaying Message Dialogs * JOptionPane.showMessageDialog(null, "Hello World"); This displays a box that says Hello World and an OK * Displaying Input Dialogs * String name; name = JOptionPane.showInputDialog("Enter your name."); This displays a box saying Enter your name, a text box to input an answer, and OK/Cancel

The String Class

The String class allows you to create objects for holding strings. It also has various methods that allow you to work with strings.

Char Data Type

The char data type is used to store characters. A variable of the char data type can hold one character at a time. Character literals are enclosed in Single Quotation Marks. *Example* char letter; letter = 'A'; System.out.println(letter);

Print and println & Java API

The print and println methods are used to display text output. They are part of the Java API, which is a collection of prewritten classes and methods for performing specific operations. * Example * System.out.println("Programming is great fun!"); System is a class that is part of Java API. One of the objects contained in the System class is named out. The Out object has methods, such as

Arithmetic Operators

Three types of operators: unary, binary, ternary - Example of unary is -5 and -number. The negative in front of the 5 and number is a unary operator. This is called the negation operator. (+) Addition Binary Operator (-) Subtraction Binary Operator (*) Multiplication Binary Operator (/) Division Binary Operator (%) Modulus Binary Operator

Local variables

Variables that are declared inside a method


संबंधित स्टडी सेट्स

Chemical Science: Unit 1 Extra Credit Quiz

View Set

Ch. 22: Neurodevelopmental Disorders

View Set

Peds Exam 2-Chapter 48; Musculoskeletal or Articular Dysfunction

View Set

Life-Span Development Santrock Chapter 10

View Set

Chapter 10: Making Capital Investment Decisions

View Set

NUR240 Ch.32 Care for Cardiac Patients

View Set

Final Business Economics Final Exam Review

View Set