Basic Syntax
Programs made up of statements
- statements end with a semicolon
- parts separated by zero or more witespaces
System.out.println("Welcome to Java");
Reasons for writing comments
for wirting human readable notes
to hide the code from compiler
Types of comments
// line comments
/* Block comment
Text ignored within the block
*/
/**
Javadoc comment
compiler treats similar to block comment,
can be used to generate documentation
*/
Packages in Java
Packages provide organization Follow standard naming convention Affect source code structure
Package name conventions
- All lowercase.
- use reverse domain name notation to assure global uniqueness.
- add further qualifiers to assure uniqueness within a company or group
package com.pluralsight; package com.pluralsight.search; package com.pluralsight.sales; package com.pluralsight.sales.accountmanagement;
Type names are qualified by their package
com.pluralsight.example.Main
package com.pluralsight.example;
public class Main {
public static void main(String[] args){
//....
}
}
Package Names Affect Source Code Organization
Pakcage names affect the souce code organization src->com->pluralsight->example->Main.java
Variables
/*
Named data storage
Strongly typed
*/
int dataValue;
dataValue = 100;
int myInfo = 200;
/*
Variable Naming
Use only letters and numbers
first charcter cannot be a number
*/
int total;
int grade4;
/*
Style names using camel case
start each word after the first with uppercase
all other letters are lower case
*/
int sum;
int studentCount;
int bankAccountBalance;
int level12Training;
// unintalized, only declared variable example;
int myVar;
// intialized variable
myVar = 50;
/*v
variables can be declared final
values cannot be changed once set
helps avoid errors caused by indadvertent variable changes
*/
final int maxStudents = 25;
// final variable can be declared without intialization, but once the value is set compiler will not be allow us to change it again.
final int someVariable;
int someOtherVariable = 100;
someVariable = someOtherVariable;
Primitive Data Types
- Built into the language
- Foundation of all the other types
- Four Categories
- Integer
- Floating point
- Character
- Boolean
Integer
Type | bits | Min Value | max Value | Literal form |
---|---|---|---|---|
byte | 8 | -128 | 127 | 0 |
short | 16 | -32768 | 32767 | 0 |
int | 32 | -2147483648 | 2147483647 | 0 |
long | 64 | -9223372036854775808 | 9223372036854775807 | 0L |
Floating Point Types
store values containing a fraction portion. Literal values placed between single qoutes. for unicode code points, use \u followed by 4-digit hex value
Type | bits | Min Value | max Value | Literal form |
---|---|---|---|---|
float | 32 | 1.4 * 10 rais to power -45 | 3.4 * 10 raise to power 38 | 0.0f |
double | 64 | 4.9 * 10 rais to power -324 | 1.7 * 10 raise to power 308 | 0.0 or 0.0d |
Character Data Types
Stores a single unicode charcter Literal values placed between single quotes
char regular = 'U';
char accentedU = '\u00DA';
Boolean Type
Stores ture/false values Literal values are true and false
Primitive data types are stored by values
It means that each variable gets its own independent copy of each value.
int firstValue = 100;
// otherValue varriable will have it's own value which is hundred in this case and otherValue and firstValue has no link or relationship after assignment.
int otherValue = firstValue;
Arithmetic Operators
Basic
Produce a result, No impact on values used in the operation.
Prefix/Postfix
Increase or decrease a value Replace orignal value.
++ Increment value by 1
– Dccrement value by 1
Order Matter Prefix applies operation before returning value ``+
Postfix applies operation after returning value
Compound Assignment
operate on a value replace orignal value