SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
Acquiring a solid foundation in Java
Omar Rivera
- Legal Identifiers
- Define Classes
● source file declaration rules,
● java/javac commands,
● main method,
● import statements,
● static import,
● class declaration and modifiers
- Distinguish between object references and primitives
- Declaring and initializing variables
- Understanding variable scope
OCA Java SE 8 Programmer I Exam Guide (Exams 1Z0-808)
AGENDA
IDENTIFIERS AND KEYWORDS
Legal identifiers
- Identifiers must start with a letter, a currency character ($), or a connecting
character such as the underscore (_). Identifiers cannot start with a digit!
- After the first character, identifiers can contain any combination of letters, currency
characters, connecting characters, or numbers.
- In practice, there is no limit to the number of characters an identifier can contain.
- You can’t use a Java keyword as an identifier.
DEFINE CLASES
Source file declaration rules
○ Only one public class per source code file.
○ Comments can appear at the beginning or end of any line in the source code file;
○ If there is a public class in a file, the name of the file must match the name of the public
class.
○ If the class is part of a package, the package statement must be the first line in the
source code file, before any import statements that may be present.
○ If there are import statements, they must go between the package statement.
○ Import and package statements apply to all classes within a source code file.
○ A file can have more than one non-public class.
○ Files with no public classes can have a name that does not match any of the classes in
the file.
DEFINE CLASES
Using the java and javac commands
● Compiling with javac
The javac command is used to invoke Java’s compiler.
Usage: javac <options> <source files>
Both the [options] and the [source files] are optional parts, and both allow multiple entries.
Sample: javac -help
javac -version Jug.java Bolivia.java
● Launching applications with java
The java command is used to invoke the Java Virtual Machine (JVM).
Usage: java <options> class <args>
The [options] and [args] parts are optional, and they can both have multiple values.
Sample: java -showversion MyClass x 1
DEFINE CLASES
Using public static void main(String[ ] args)
main() is the method that the JVM uses to start execution of a Java program.
public static void main(String[] args)
Other versions of main() with other signatures are perfectly legal, but they’re treated as normal
methods.
The order of its modifiers can be altered a little; the String array doesn’t have to be named args; and
it can be declared using var-args syntax.
static public void main(String[] args) {}
public static void main(String... args) {}
public static void main(String[] parameters) {}
DEFINE CLASES
Import Statements and the Java API
Sample class: public class ArrayList {
static public void main(String... parameters) {
System.out.println("Fake ArrayList class");
}
}
The fully qualified name
java.util.ArrayList
The import format:
import java.util.ArrayList;
import java.util.*; // wildcard character (*)
DEFINE CLASES
Static Import Statements
The syntax MUST be import static followed by the fully qualified name of the static member you
want to import, or a wildcard
import static java.lang.System.out;
import static java.lang.Integer.*;
The wildcard (*) import ALL the static members in the class.
You can do a static import on static object references, constants (remember they’re static and final),
and static methods.
DEFINE CLASES
Class Declarations and Modifiers
● Access modifiers (public, protected, private)
● Nonaccess modifiers (including strictfp, final, and abstract)
* The fourth access control level (called default or package access)
Class Access
When we say code from one class (class A) has access to another class (class B), it means class A can
do one of three things:
● Create an instance of class B.
● Extend class B (in other words, become a subclass of class B).
● Access certain methods and variables within class B, depending on the access control of those
methods and variables.
DEFINE CLASES
Final Classes
The the final keyword means the class can’t be subclassed. In other words, no other class can ever
extend (inherit from) a final class.
Abstract Classes
An abstract class can never be instantiated. Its sole purpose, mission in life, is to be extended
(subclassed).
ASSIGNMENTS
Stack and Heap
Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in
the computer's RAM
● Instance variables and objects live on the heap.
● Local variables live on the stack.
ASSIGNMENTS
Literal Values for All Primitive Types
A primitive literal is merely a source code representation of the primitive data types
‘b’ // char literal
42 // int literal
false // boolean literal
7421.777 // float literal
ASSIGNMENTS
Numeric Literals with Underscores
As of Java 7, numeric literals can be declared using underscore characters (_), ostensibly to improve
readability.
int pre7 = 1000000 // pre java 7
int with7 = 1_000_000 // much clearer
int a = _1_000_000 // illegal
int w = 10_0000_0 // legal but confusing
The main rule you have to keep track of is that you CANNOT use the underscore literal at the
beginning or end of the literal
ASSIGNMENTS
Integer Literals
There are four ways to represent integer numbers in the Java language: decimal (base 10), octal
(base 8), hexadecimal (base 16), and, as of Java 7, binary (base 2).
Decimal Literals
int g = 17;
Binary Literals
Also new to Java 7 is the addition of binary literals. Binary literals can use only the digits 0 and 1.
Binary literals must start with either 0B or 0b, as shown:
int f1 = 0B101010; // set f1 to binary 101010 (decimal 42)
int g2 = 0b00011; // set g2 to 11 (decimal 3)
ASSIGNMENTS
Octal Literals
Octal integers use only the digits 0 to 7. In Java, you represent an integer in octal form by placing a
zero in front of the number
int six = 06; // decimal 6
int seven = 07; // decimal 7
int eight = 010; // decimal 8
Hexadecimal Literals
Hexadecimal (hex for short) numbers are constructed using 16 distinct symbols. Prefix 0x (or 0X)
int f = 0X0001;
int g = 0x7fffff;
int fg = 0xDeadCafe;
ASSIGNMENTS
All four integer literals (binary, octal, decimal, and hexadecimal) are defined as int by default, but
they may also be specified as long by placing a suffix of L or l after the number:
long slx = 06L;
long deore = 0x7fffffl;
Floating-point Literals
Floating-point numbers are defined as a number, a decimal symbol, and more numbers representing
the fraction. If you want to assign a floating-point literal to a variable of type float (32 bits), you must
attach the suffix F or f to the number. If you don’t do this, the compiler will complain about a possible
loss of precision,
float f = 23.467890 // compiler error, possible loss of precision
float g = 48929379.02989F // OK, has the sufix F
ASSIGNMENTS
You may also optionally attach a D or d to double literals, but it is not necessary because this is the
default behavior.
double fab = 23.467890D // optional D, not required
double gab = 4779.029 // NO D sufix, OK
Boolean Literals
A boolean value can be defined only as true or false
boolean a = true;
boolean c = 0; // Compiler error
ASSIGNMENTS
Character Literals
A char literal is represented by a single character in single quotes, characters are just 16-bit
unsigned integers under the hood.
char a = ‘a’;
char b = ‘@’;
You can also type in the Unicode value of the character, using the Unicode notation of prefixing the
value with u :
char letterN = ‘u004E’; // The letter ‘N’
You can assign a number literal, assuming it will fit into the unsigned 16-bit range (0 to 65535)
char a = 0x892; // hexadecimal char b = 747; // int literal
char c = (char) 70000 // 70000 is out of range char d = (char) -96;
ASSIGNMENTS
You can also use an escape code (the backslash) if you want to represent a character that can’t be
typed in as a literal, including the characters for linefeed, newline, horizontal tab, backspace, and
quotes:
char c = ‘”’;
char d = ‘n’;
char tab = ‘t’;
Literal Values for Strings
A string literal is a source code representation of a value of a String object.
String go = “Hello Jug”;
ASSIGNMENTS
Assigning One Primitive Variable to Another Primitive Variable
When you assign one primitive variable to another, the contents of the right-hand variable are
copied
int a = 10;
int b = a;
b = 40;
// a ????
ASSIGNMENTS
Assigning One Primitive Variable to Another Primitive Variable
When you assign one primitive variable to another, the contents of the right-hand variable are
copied
int a = 10;
int b = a;
b = 40;
// a ????
ASSIGNMENTS
Reference Variable Assignments
You can assign a newly created object to an object reference variable
Button b = new Button();
● Makes a reference variable named b, of type Button
● Creates a new Button object on the heap
● Assigns the newly created Button object to the reference variable b
ASSIGNMENTS
ASSIGNMENTS
ASSIGNMENTS
Variable Scope
For the purposes of discussing the scope of variables, we can say that there are four basic scopes:
1. Static variables have the longest scope; they are created when the class is loaded, and they
survive as long as the class stays loaded in the Java Virtual Machine (JVM).
2. Instance variables are the next most long-lived; they are created when a new instance is created,
and they live until the instance is removed.
3. Local variables are next; they live as long as their method remains on the stack. As we’ll soon see,
however, local variables can be alive and still be “out of scope.”
4. Block variables live only as long as the code block is executing.
ASSIGNMENTS
Variable Initialization
Default Values for Primitives and Reference Types
ASSIGNMENTS
Passing Variables into Methods
ASSIGNMENTS
Passing Primitive Variables

Más contenido relacionado

La actualidad más candente

Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
valarpink
 
Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_language
SINGH PROJECTS
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
Ooabap notes with_programs
Ooabap notes with_programsOoabap notes with_programs
Ooabap notes with_programs
Kranthi Kumar
 

La actualidad más candente (19)

Python Programming
Python ProgrammingPython Programming
Python Programming
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Declaration of variables
Declaration of variablesDeclaration of variables
Declaration of variables
 
C language basics
C language basicsC language basics
C language basics
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Fundamentals of c programming
Fundamentals of c programmingFundamentals of c programming
Fundamentals of c programming
 
Opps concept
Opps conceptOpps concept
Opps concept
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_language
 
Ch6
Ch6Ch6
Ch6
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)
 
Ooabap notes with_programs
Ooabap notes with_programsOoabap notes with_programs
Ooabap notes with_programs
 

Similar a Acquiring a solid foundation in java

Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
Connex
 
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
GANESHBABUVelu
 
2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment
CarlosPineda729332
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in java
Shashwat Shriparv
 

Similar a Acquiring a solid foundation in java (20)

a basic java programming and data type.ppt
a basic java programming and data type.ppta basic java programming and data type.ppt
a basic java programming and data type.ppt
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
java vs C#
java vs C#java vs C#
java vs C#
 
Difference between Java and c#
Difference between Java and c#Difference between Java and c#
Difference between Java and c#
 
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
 
2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment2013 bookmatter learn_javaforandroiddevelopment
2013 bookmatter learn_javaforandroiddevelopment
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in java
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
 
JAVA(module1).pptx
JAVA(module1).pptxJAVA(module1).pptx
JAVA(module1).pptx
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
lecture 6
 lecture 6 lecture 6
lecture 6
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer science
 

Último

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Último (20)

Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 

Acquiring a solid foundation in java

  • 1. Acquiring a solid foundation in Java Omar Rivera
  • 2. - Legal Identifiers - Define Classes ● source file declaration rules, ● java/javac commands, ● main method, ● import statements, ● static import, ● class declaration and modifiers - Distinguish between object references and primitives - Declaring and initializing variables - Understanding variable scope OCA Java SE 8 Programmer I Exam Guide (Exams 1Z0-808) AGENDA
  • 3. IDENTIFIERS AND KEYWORDS Legal identifiers - Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore (_). Identifiers cannot start with a digit! - After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers. - In practice, there is no limit to the number of characters an identifier can contain. - You can’t use a Java keyword as an identifier.
  • 4. DEFINE CLASES Source file declaration rules ○ Only one public class per source code file. ○ Comments can appear at the beginning or end of any line in the source code file; ○ If there is a public class in a file, the name of the file must match the name of the public class. ○ If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present. ○ If there are import statements, they must go between the package statement. ○ Import and package statements apply to all classes within a source code file. ○ A file can have more than one non-public class. ○ Files with no public classes can have a name that does not match any of the classes in the file.
  • 5. DEFINE CLASES Using the java and javac commands ● Compiling with javac The javac command is used to invoke Java’s compiler. Usage: javac <options> <source files> Both the [options] and the [source files] are optional parts, and both allow multiple entries. Sample: javac -help javac -version Jug.java Bolivia.java ● Launching applications with java The java command is used to invoke the Java Virtual Machine (JVM). Usage: java <options> class <args> The [options] and [args] parts are optional, and they can both have multiple values. Sample: java -showversion MyClass x 1
  • 6. DEFINE CLASES Using public static void main(String[ ] args) main() is the method that the JVM uses to start execution of a Java program. public static void main(String[] args) Other versions of main() with other signatures are perfectly legal, but they’re treated as normal methods. The order of its modifiers can be altered a little; the String array doesn’t have to be named args; and it can be declared using var-args syntax. static public void main(String[] args) {} public static void main(String... args) {} public static void main(String[] parameters) {}
  • 7. DEFINE CLASES Import Statements and the Java API Sample class: public class ArrayList { static public void main(String... parameters) { System.out.println("Fake ArrayList class"); } } The fully qualified name java.util.ArrayList The import format: import java.util.ArrayList; import java.util.*; // wildcard character (*)
  • 8. DEFINE CLASES Static Import Statements The syntax MUST be import static followed by the fully qualified name of the static member you want to import, or a wildcard import static java.lang.System.out; import static java.lang.Integer.*; The wildcard (*) import ALL the static members in the class. You can do a static import on static object references, constants (remember they’re static and final), and static methods.
  • 9. DEFINE CLASES Class Declarations and Modifiers ● Access modifiers (public, protected, private) ● Nonaccess modifiers (including strictfp, final, and abstract) * The fourth access control level (called default or package access) Class Access When we say code from one class (class A) has access to another class (class B), it means class A can do one of three things: ● Create an instance of class B. ● Extend class B (in other words, become a subclass of class B). ● Access certain methods and variables within class B, depending on the access control of those methods and variables.
  • 10. DEFINE CLASES Final Classes The the final keyword means the class can’t be subclassed. In other words, no other class can ever extend (inherit from) a final class. Abstract Classes An abstract class can never be instantiated. Its sole purpose, mission in life, is to be extended (subclassed).
  • 11. ASSIGNMENTS Stack and Heap Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM ● Instance variables and objects live on the heap. ● Local variables live on the stack.
  • 12. ASSIGNMENTS Literal Values for All Primitive Types A primitive literal is merely a source code representation of the primitive data types ‘b’ // char literal 42 // int literal false // boolean literal 7421.777 // float literal
  • 13. ASSIGNMENTS Numeric Literals with Underscores As of Java 7, numeric literals can be declared using underscore characters (_), ostensibly to improve readability. int pre7 = 1000000 // pre java 7 int with7 = 1_000_000 // much clearer int a = _1_000_000 // illegal int w = 10_0000_0 // legal but confusing The main rule you have to keep track of is that you CANNOT use the underscore literal at the beginning or end of the literal
  • 14. ASSIGNMENTS Integer Literals There are four ways to represent integer numbers in the Java language: decimal (base 10), octal (base 8), hexadecimal (base 16), and, as of Java 7, binary (base 2). Decimal Literals int g = 17; Binary Literals Also new to Java 7 is the addition of binary literals. Binary literals can use only the digits 0 and 1. Binary literals must start with either 0B or 0b, as shown: int f1 = 0B101010; // set f1 to binary 101010 (decimal 42) int g2 = 0b00011; // set g2 to 11 (decimal 3)
  • 15. ASSIGNMENTS Octal Literals Octal integers use only the digits 0 to 7. In Java, you represent an integer in octal form by placing a zero in front of the number int six = 06; // decimal 6 int seven = 07; // decimal 7 int eight = 010; // decimal 8 Hexadecimal Literals Hexadecimal (hex for short) numbers are constructed using 16 distinct symbols. Prefix 0x (or 0X) int f = 0X0001; int g = 0x7fffff; int fg = 0xDeadCafe;
  • 16. ASSIGNMENTS All four integer literals (binary, octal, decimal, and hexadecimal) are defined as int by default, but they may also be specified as long by placing a suffix of L or l after the number: long slx = 06L; long deore = 0x7fffffl; Floating-point Literals Floating-point numbers are defined as a number, a decimal symbol, and more numbers representing the fraction. If you want to assign a floating-point literal to a variable of type float (32 bits), you must attach the suffix F or f to the number. If you don’t do this, the compiler will complain about a possible loss of precision, float f = 23.467890 // compiler error, possible loss of precision float g = 48929379.02989F // OK, has the sufix F
  • 17. ASSIGNMENTS You may also optionally attach a D or d to double literals, but it is not necessary because this is the default behavior. double fab = 23.467890D // optional D, not required double gab = 4779.029 // NO D sufix, OK Boolean Literals A boolean value can be defined only as true or false boolean a = true; boolean c = 0; // Compiler error
  • 18. ASSIGNMENTS Character Literals A char literal is represented by a single character in single quotes, characters are just 16-bit unsigned integers under the hood. char a = ‘a’; char b = ‘@’; You can also type in the Unicode value of the character, using the Unicode notation of prefixing the value with u : char letterN = ‘u004E’; // The letter ‘N’ You can assign a number literal, assuming it will fit into the unsigned 16-bit range (0 to 65535) char a = 0x892; // hexadecimal char b = 747; // int literal char c = (char) 70000 // 70000 is out of range char d = (char) -96;
  • 19. ASSIGNMENTS You can also use an escape code (the backslash) if you want to represent a character that can’t be typed in as a literal, including the characters for linefeed, newline, horizontal tab, backspace, and quotes: char c = ‘”’; char d = ‘n’; char tab = ‘t’; Literal Values for Strings A string literal is a source code representation of a value of a String object. String go = “Hello Jug”;
  • 20. ASSIGNMENTS Assigning One Primitive Variable to Another Primitive Variable When you assign one primitive variable to another, the contents of the right-hand variable are copied int a = 10; int b = a; b = 40; // a ????
  • 21. ASSIGNMENTS Assigning One Primitive Variable to Another Primitive Variable When you assign one primitive variable to another, the contents of the right-hand variable are copied int a = 10; int b = a; b = 40; // a ????
  • 22. ASSIGNMENTS Reference Variable Assignments You can assign a newly created object to an object reference variable Button b = new Button(); ● Makes a reference variable named b, of type Button ● Creates a new Button object on the heap ● Assigns the newly created Button object to the reference variable b
  • 25. ASSIGNMENTS Variable Scope For the purposes of discussing the scope of variables, we can say that there are four basic scopes: 1. Static variables have the longest scope; they are created when the class is loaded, and they survive as long as the class stays loaded in the Java Virtual Machine (JVM). 2. Instance variables are the next most long-lived; they are created when a new instance is created, and they live until the instance is removed. 3. Local variables are next; they live as long as their method remains on the stack. As we’ll soon see, however, local variables can be alive and still be “out of scope.” 4. Block variables live only as long as the code block is executing.
  • 26. ASSIGNMENTS Variable Initialization Default Values for Primitives and Reference Types