[Java] Introduction

I have to use Java, so for memos of various methods After all it is Python

Java environment construction on Mac

Agree, download and install at here Open the terminal

java

If there is no error, the environment construction is completed

How to compile and run

Please try to make this file first

Test.java


public class Test{
     public static void main(String[] args){
          System.out.print("hello\n");
     }
}

compile

$ javac Test.java

Run

$ Java Test

output


hello

variable

VariableTest.java


public class VariableTest{
	public static void main(String[] args){
		int age = 35;
		System.out.print(age+"\n");
	}
}

output


35

if statement

Greeting.java


public class Greeting{
   public static void main(String[] args){
      //int time = 8;
      //System.out.println("The current time is" + time + "It's time");
      //System.out.println("Good morning");
     int time = 8;
     if((time >= 4) && (time <= 10)){
       System.out.println("The current time is" + time + "It's time");
       System.out.println("Good morning");
     }else if((time >= 11) && (time <= 17)){
       System.out.println("The current time is" + time + "It's time");
       System.out.println("Hello");
     }else if(time == 18){
       System.out.println("The current time is" + time + "It's time");
       System.out.println("Good evening");
     }else if((time >= 19) && (time <= 3)){
       System.out.println("The current time is" + time + "It's time");
       System.out.println("good night");
     }
   }
}

output


The current time is 8 o'clock
Good morning

Array (list)

Scores.java


public class Scores{
     public static void main(String[] args){
          //int scoreA;
          //int scoreB;
          //int scoreC;
          //int scoreD;
          int[] scores = new int[]{80,65,70,95};

          System.out.println("Mr. A's score:" + scores[0] + "point");
          System.out.println("Mr. B's score:" + scores[1] + "point");
          System.out.println("Mr. C's score:" + scores[2] + "point");
          System.out.println("Mr. D's score:" + scores[3] + "point");
     }
}

output


Mr. A's score: 80 points
Mr. B's score: 65 points
Mr. C's score: 70 points
Mr. D's score: 95 points

String

Greeting.java


public class Greeting{
   public static void main(String[] args){
      int time = 8;
      String message = "The current time is" + time + "It's time";
      String greeting = "";

      if((time >= 4) && (time <= 10)){
         greeting = "Good morning";
      }else if((time >= 11) && (time <= 17)){
         greeting = "Hello";
      }else if(time == 18){
         greeting = "Good evening";
      }else if((time>=18 && time<=24) || (time<=3 && time>=0)){
         greeting = "good night";
      }else{
         message = "Enter a value from 0 to 24 for time";
      }

      System.out.println(message);
      System.out.println(greeting);
   }
}

output


The current time is 8 o'clock
Good morning

for statement

PowerOfTwo.java


//coding:utf-8
public class PowerOfTwo{
     public static void main(String[] args){
          int n = 5;
          int answer = 1;

          for( int i=0; i<5; i++ ){ 
               answer = answer * 2; 
          } 

          System.out.print("2 to the 5th power");
          System.out.println(answer);
     }
}

output


2 to the 5th power is 32

Index specification with for statement

  1. Get the length of the array
  2. For the length of the array you put out
  3. Specify the element number (index) with the changing i

Scores.java


public class Scores{
     public static void main(String[] args){
          String[] names = new String[]{"Aoki","Iida","Ueda","Eto"};
          int[] scores = new int[]{80,65,70,95};

          for(int i=0; i< names.length; i++){
               System.out.println(names[i] + "Score:" + scores[i] + "point");
          }
     }
}

output


Aoki's score: 80 points
Mr. Iida's score: 65 points
Ueda's score: 70 points
Eto's score: 95 points

Function (method)

Same as Python main runs automatically

No arguments

*** sub *** (created function) is executed by calling

MethodTest.java


//coding:utf-8
class MethodTest{
   public static void main(String[] args){
      System.out.println("main method was called");
      sub();
   }
   public static void sub(){
      System.out.println("sub method was called");
   }
}

output


main method was called
sub method was called

With arguments

N squared

Calcu.java


//coding:utf-8
public class Calcu{
     public static void main(String[] args){
          twice(5);
          twice(65);
          twice(3247);
          powerOfTwo(3);
          powerOfTwo(10);
     }

     public static void twice(int n){
          System.out.print(n + "Twice as");
          System.out.println(n * 2);
     }

     public static void powerOfTwo(int n){
          int answer = 1;

          for( int i=0; i<n; i++ ){ 
               answer = answer * 2;
          } 

          System.out.print("2 of" + n + "Multiplication is");
          System.out.println(answer);
     }
}

output


Twice as 5 is 10
Twice as 65 is 130
Twice as much as 3247, 6494
2 to the 3rd power is 8
2 to the 10th power is 1024

There is a return value

ReturnTest.java


public class ReturnTest{
   public static void main(String[] args){
      int a = 23165247;
      if( isMultipleOf3(a) ){
         System.out.println(a + "Is divisible by 3");
      }else{
         System.out.println(a + "Is not divisible by 3");
      }
   }
   public static boolean isMultipleOf3(int n){
      boolean result;
      result = ( n % 3  == 0 );
      return result;
   }
}

output


23165247 is divisible by 3

Create a function with the return value

ReturnTest.java


public class ReturnTest{
   public static void main(String[] args){
           System.out.println(isMultipleOf3(123456789));
     }

    //When returning as a String
   public static String isMultipleOf3(int n){
     String resultMessage = "";
     if(n % 3 == 0){
          resultMessage = n + "Is divisible by 3";
     }else{
          resultMessage = n + "Is not divisible by 3";
     }
     return resultMessage;
   }
}

ReturnTest.java


public class ReturnTest{
   public static void main(String[] args){
      int a = 23165247;
      if( isMultipleOf3(a) ){
         System.out.println(a + "Is divisible by 3");
      }else{
         System.out.println(a + "Is not divisible by 3");
      }
   }
   //If it returns True or False
   public static boolean isMultipleOf3(int n){
      boolean result;
      result = ( n % 3  == 0 );
      return result;
   }
}

By the way, *** void *** is used when no return value is returned.

output


123456789 is divisible by 3

Command line argument input

CommandLine.java


public class CommandLine{
   public static void main(String[] args){
      for(int i=0; i<args.length; i++){
         System.out.println(args[i]);
      }
   }
}

Try running with this

$ java CommandLine hello 1 2 3 shuto

result


hello
1
2
3
shuto

Successful input from the command line with no restrictions on arguments

Recommended Posts

[Java] Introduction
Introduction to java
Java Performance Chapter 1 Introduction
Java
Introduction to java command
Java
[Java] Introduction to lambda expressions
[Java] Introduction to Stream API
[Introduction to rock-paper-scissors games] Java
Lombok ① Introduction
Introduction (self-introduction)
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
100% Pure Java BDD with JGiven (Introduction)
java (override)
java (method)
Java Day 2018
Java string
[Introduction to Java] About lambda expressions
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
[Introduction to Java] About Stream API
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
Java array
Studying Java ―― 1
[Java] Array
Let's use Twilio in Java! (Introduction)
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Introduction to Functional Programming (Java, Javascript)
Java features
java beginner 3
Java memo
java (encapsulation)