Java - Data Types and Operators

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

Assigning variables

Enter type var = value int count = 10 // give count an initial value of 10 char ch = 'X' // initialize ch with the letter X

Hexadecimal, octal, and binary literals

Number system based on 8 = octal, uses 0 through 7, must begin wiht 0 oct = 011 // 9 in decimal Number system based on 16 = hexadecimal - uses digits 0-9 and letters A-F for 10-15. Must begin with 0x or 0X. hex = 0xFF = 255 in decimal

Eight primitive types

boolean - True/False byte - 8 bit interger char - Character double - Double-precision floating point float - Single-precision floating point int - integer long - long integer short - short integer

Short circuit operators

special version of AND and OR for efficient code AND, if the first operand is false, the outcome is false OR, if the first outcome is true, the rest doesn't matter Short circuit versions only evaluate the second operand when necessary

Arithmetic operators

+ - * / % ++ -- Can also be used on type char If dividing, the reaminder is truncated , 10/3 = 3. For the remainder, enter 10%3

short

16 bits wide -32,768 to +32767

int

32 bits wide -2,147,483,648 to +2,147,483,647

long

64 bits wide -9,223,372,036,854,775,808 to + 9,223,372,036,854,775,807

byte

8 bits long -128 to +127 range

Scope

A block defines a scope A scope determines what objects are visible to your program and the lifetime of those objects. The scope defined by a method begins with its opening curly brace. Generally inside the scope = not visible to code outside of scope Variables are only valid after they're declared/if it's in the block they can be accessed. Outside of scope = variable doesn't exist

String literals

A string is a set of characters enclosed by double quotes like "this is a test" Can combine with escape sequences like "First line \nSecond line" would use \n to generate a new line. or "A\tB\tC" = A tab B tab C

Data type

All variables, expressions and values are assigned a type. The operations are type checked by the compiler for type compatibility and what operators are allowed.

Object Oriented Data Types

Are defined by classes

Comparison operators

Can only be applied to those types that support ordering realtionship. Relational operators can be applied to all numberic types and char boolean can only be compared for equality or inequality

Literals

Fixed values also called constants Can be any primitive data type Integer literals - default type is int and can have underscores

Relational operators

How things related to each other == != > < >= <=

Dynamic initialization

Java allows variables to be initiliazed dynamically, using any expression valid at the time the variable is declared. Ex. doule volume = 3.14 * radius * radius * height; Radius is initliazed by constants and volume is initilized dynamically

Char details

Java uses unicode = all the characters in the human language Enclose the value assigned to char in quotes char ch ch = 'x' characters can be incremented. ch++ would give you a value of y, then z.

Math

Math is a class Have math. precede math functions like sqrt.

Operators

Math or logical equation Four classes Arithmetic bitwise relational logical

Primitive details

Primitive types have a range and behavior for each type

Boolean details

Represents true/false details When a boolean value is output by println(), true or false is displayed The value of a boolean variable controls the if statement so you don't need to enter if (b==true) The outcome of a relational operator like < is a boolean value, which is why 10>9 displays as "true".

Logical operators

True and false can be connected & | = OR ^ = XOR (exclusive OR) || = short circuit OR && short circuit AND ! NOT

float and double details

Used for flowting piont types float is 32 bits wide double is 64 bits wide double is used for math

byte details

Useful with binary data

Increments and decrements

X= X + 1 is X++ X = X - 1 is X-- X= X+1 is ++X or X++ X = 10, Y = ++X, Y would be 11 X = 10, Y = X++, Y would still be 10, X would be 11

Character escape sequences

\' = single quote \"" = double quote \\ = backslash \r = carriage return \n = new line \f = farm feed \t = horizontal tab \b = backspace \ddd = Octal constant where ddd is an actual constant \uxxx = Hexadecimal constant where xxxx is a hexadecimal constant Can assign these to values like ch = '\t'; will assign ch the ta character

Logical operands must be type

boolean

int details

int are often used in control loops, index arrays, and math. If you need an interger greater than int, use long.


Ensembles d'études connexes

05.01 Overview of the Nursing Process

View Set

CompTIA Network+ Certification Exam N10-007 Practice Test 8

View Set

Investigating Currents and Fields Quick Check

View Set

Health Policy Provisions, Clauses, and Riders

View Set

Chapter 1 Notes - Chapter Recap Questions

View Set