Introduction
static
.this.variable
class name.variable name
variables
are called fields
.Pascal method
PermissionsIntro
AsyncTask
FileCopy
MainActivity
camelCase method (lower camel Case)
isEmpty()
hasChanged()
canAccess()
camelCase method (lower camel Case)
count
errorMsg
snake case
MAX_LENGTH
DIR_PATH
'
is used for one character"
Use for stringschar a = 'a';
String s = "hello world!";
int i = 10;
long l = 10000000L;
double d = 234.222;
float f = 234.987F;
int[] array;
array = new int[3];
int[] array;
array = new int[] {100, 200, 300};
int[] array = {100, 200, 300};
int i = array.length;
System.out.println(); //With line breaks
System.out.print(); //No line breaks
System.err.println(); //With line breaks
System.err.print(); //No line breaks
x++;
x--;
x += 1;
x -= 1;
String s = String.valueOf(i);
int i = Integer.parseInt(s);
double d = (double)i;
int i = (int)d;
if
if (conditions){
//processing
} else if (conditions){
//processing
} else {
}
switch
switch(variable){
case value 1:
//processing
//Process when the value of the variable is "value 1".
break;
case value 2:
case value 3:
//processing
//Process when the value of the variable is "value 2" or "value 3".
break;
default:
//processing
break;
}
conditions?Return value when positive:Return value in case of mistake;
Example:
String msg = i > 80 ? "good" : "so so...";
break;
⇒ Exit the loop.continue;
⇒ Go through the current loop times and proceed to the next loop times.while
while (conditions){
//processing
}
do while
do {
//processing
} while (conditions)
for
for (int i = 0; i < 10; i++){
//processing
}
String[] names = {"Fujiwara", "Sakai", "Daiwa"};
for (String name : names) {
System.out.println(name);
}
Target | Order |
---|---|
field | public protected private static final transient volatile |
Method | public protected private abstract static final synchronized native strictfp |
class | public protected abstract static final strictfp |
Access modifier | Description |
---|---|
private | Only accessible from the same class |
protected | Accessable from current class and subclass |
None | Accessable from a class in the same package as the current class |
public | Accessable from anywhere |
Qualifier Return type method name(argument){
Method processing
}
public static void SayHello(){
//processing
}
public static void main(String[] args){
}
public class MyApp{
public static void sayHi(String name){
System.out.println("Hi! " +name);
}
public static void sayHi(){
System.out.println("Hi! Nobody");
}
public static void main(String[] args){
sayHi("Jones"); // Hi! Jones
sayHi(); // Hi! Nobody
}
}
public class User {
public void hello() {
System.out.println("Hello World!");
}
}
public class AdminUser extends User {
@Override //Annotation
public void hello() {
System.out.println("Hello World override!");
}
}
Qualifier class class name{
}
class User {
}
User jones = new User();
class User {
void sayHi(){
System.out.println("hi!");
}
}
class AdminUser extends User {
}
Description | Description |
---|---|
Variable name |
Calling local variables |
this.Variable name |
Calling instance variables ⇒ At the time of declarationstatic Variables not attached |
name of the class.Variable name |
Calling class variables ⇒ At the time of declarationstatic The one I put on |
new class name ()
.class name ()
//Access class name(){}
//You don't need a return type because you can't return a return value
public class User{
public User(String name){
//processing
}
}
this()
this ()
written in the class means a constructor. It can be used when calling the constructor somewhere in the class. Or when overloading the constructor.class User{
private String name;
User(String name){
this.name = name;
}
User() {
this("no name");
}
}
super()
public class User {
protected String name;
public User(String name){
this.name = name;
}
}
public class AdminUser extends User {
public AdminUser(){
super("AdminUser desu"); // User(String name){}call
//Special processing only for child class here
}
}
class User {
private static int count; //Class variables
static {
User.count = 0;
}
}
class User {
private static int count; //Class variables
{
System.out.println("Instant Initializer")
}
}
Construction
/(root)
├── README.md
└── com
└── dotinstall
└── myapp
├── MyApp.java
└── model
├── AdminUser.java
└── User.java
MyApp.java
package com.dotinstall.myapp.model;
import
MyApp.java
import com.dotinstall.myapp.model.User;
MyApp.java
import com.dotinstall.myapp.modem.*;
▼ interface
variable
described in interface is forcibly given with public
, static
and final
.public
and ʻabstract are added to the
methodwritten with
no qualifier`.default modifier
when describing the class, you can write the processing of the method. (public
)public
)interface Printable {
double VERSION = 1.2; //constant
void print(); //Abstract method
default void getInfo() {} //default method
static static_method() {} //static method
}
class User implements Printable {
}
enum Fruit {
Banana(1, "Philippines"),
Apple(2, "Aomori"),
Orange(3, "Ehime");
private int id;
private String pref;
private Fruit(int id, String pref) {
this.id = id;
this.pref = pref;
}
public int getId() {
return this.id;
}
public String getPref() {
return this.pref;
}
}
valueOf()
Fruit frt;
frt = Fruit.Apple;
if (frt == Fruit.valueOf("Apple")) {
//processing
}
values()
for (Fruit frt : Fruit.values()) {
System.out.println(frt.getPref());
}
class MyException extends Exception {
public MyException() {
super ("error message");
}
}
class MyException extends Exception {
public MyException(String errorMessage) {
super (errorMessage);
}
}
if (b < 0) {
throw new MyException();
}
if (b < 0) {
throw new MyException("error message");
}
try {
} catch (ArithmeticException e) { //Error class+Variable name
//processing
} catch (MyException e) { //Error class+Variable name
//processing
} finally {
//processing
}
▼ Wrapper Class
Omitted
▼ Generics
FIXME
Somehow, the variable type such as the argument is made undefined so that the variable type can be selected when the Class is converted to Instance. It seems.
It seems that constant types such as ʻintand
double cannot be used, and only reference types such as ʻInteger
and Double
can be used.
When I looked it up, it seemed to be very deep, so I gave up looking it up.
class MyData<T> {
public void printIt(T x) {
System.out.println(x);
}
}
public class MyApp{
public static void main (String[] args){
MyData<Integer> i = new MyData<>();
i.printIt(29);
}
}
▼ Thread / Multi Thread
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 500; i++) {
System.out.print('.');
}
}
}
public class MyApp{
public static void main (String[] args){
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
for (int i = 0; i < 500; i++) {
System.out.print('+');
}
}
}
interface Foo {
void sampleMethod();
}
public class MyApp{
public static void main (String[] args){
Foo unNamed = new Foo() { //Here, of the class that implements Foo
@Override //Definition and instantiation
public void sampleMethod(){ //It is done at the same time.
System.out.println("print it!"); // (Foo is an interface.)
}
};
unNamed.sampleMethod();
}
}
interface Foo {
void sampleMethod(String text); //Abstract method definition
}
public class MyApp{
public static void main (String[] args){
Foo rambda = (text) -> { //Concrete method definition
System.out.println(text); //I'll do it here.
};
rambda.sampleMethod("do it !");
}
}
▼ math / random
▼ ArrayList
import java.util.*;
public class MyApp{
public static void main (String[] args){
List<Integer> sales = new ArrayList<>();
sales.add(10);
sales.add(20);
sales.add(30);
for (Integer i = 0; i < sales.size(); i++) {
System.out.println(sales.get(i));
}
for (Integer sale : sales) {
System.out.println(sale);
}
}
}
▼ HashSet
import java.util.*;
public class MyApp{
public static void main (String[] args){
Set<Integer> sales = new HashSet<>();
sales.add(10);
sales.add(20);
sales.add(30);
for (Integer sale : sales) {
System.out.println(sale);
}
}
}
▼ HashMap
import java.util.*;
public class MyApp{
public static void main (String[] args){
Map<String, Integer> students = new HashMap<>();
students.put("uta", 80);
students.put("jones", 60);
students.put("tatsuya", 70);
students.remove("jones");
System.out.println(students.get("tatsuya"));
System.out.println(students.size());
for (Map.Entry<String, Integer> student : students.entrySet()) {
System.out.println(student.getKey() + ':' + student.getValue());
}
}
}
Recommended Posts