Java
/
Basics
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 8
-
Quick start S
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
S
R
Q
Java Basics Variables
Local variables must be initialized before use Variables are passed by value, not reference class int b; // default 0 method String a = "a"Variables
Parameters
p76 The variables passed as method's parameters has to match the type.
/**
* The variables passed as method's parameters ...
* has to match the type
*/
package com.minte9.basics.variables;
public class Parameters {
public static void main(String[] args) {
Math math = new Math();
int sum = math.sum(1,2);
System.out.println(sum); // 3
//int sum2 = math.sum(3, "1"); // compile error
}
}
class Math {
public int sum(int n1, int n2) {
return n1 + n2;
}
}
Default values
p85 Object instance variables always get a default value.
/**
* Class instance variables always have default value
* Local variables must be initialized before use
*/
package com.minte9.basics.variables;
public class DefaultValues {
public static void main(String[] args) {
Values obj = new Values();
obj.showValues();
}
}
class Values {
int a; // default value 0
float b;
boolean c;
Values v;
public void showValues() {
System.out.println(a); // 0
System.out.println(b); // 0.0
System.out.println(c); // false
System.out.println(v); // null
String a = "a"; // local variable, no default value
System.out.println(a); // a
}
}
Pass by Value
p77 Variables in Java are passed by value (a copy), not by reference.
/**
* Variables in Java are passed by copy,
* not by reference
*
* x bits are copied / this copy goes in z
* z changes / x not changed
*/
package com.minte9.basics.variables;
public class PassedByValue {
public static void main(String[] args) {
new MyClass();
}
}
class MyClass {
int x;
public MyClass() {
x = 7; // 00000111
go(x); // x bits are copied in z
}
public void go(int z) {
System.out.println(x == z); // true
z = 0; // x doesn't change
System.out.println(x == z); // false
}
}
Setter
p79 Setter and getter naming convention are an important Java standard.
/**
* Setter and getter JavaBeans specifications:
*
* The setter method for foo must be called setFoo()
* The gettter method for xIndex must be called getxIndex()
*/
package com.minte9.basics.variables;
public class Setter {
public static void main(String[] args) {
Dog dog = new Dog();
dog.setName("Rex");
System.out.println(dog.getName()); // Rex
}
}
class Dog {
private String name;
public void setName(String x) {
name = x;
}
public String getName() {
return name;
}
}
➥ Questions
Last update: 61 days ago