Intro
Java is a statically typed language, which means that all variables, parameters and methods must have a declared type. After declaration , the type can never change. And before executing the code ,Java will check the type first , so it will never run into type erroe.
Basic syntax
-
All statements must end with a semi-colon ‘;’
-
Curly brace{} : which enclose section of code (functions classes and etc.)
-
Declare varible :variable must be declared before it used , and must be given a type.
-
the difference between System.out.println() and System.out.print()
- println will include a new line (or a return)
- System.out.print() must have the parameter(s) in the brackets
-
Commentary
- Java comments can either start with ‘//’ or can extend over any number of lines using “/*” and “*/”
- “/**” : documentation comments, which can be interpreted by some tools
-
Class
- Every function and variable in Java is contained in the class , which is similar as class in Python
- All code lives with classes
- All classes , in turn , belong to some package.
-
Method(Function)
- Function Headers in Java contain more information that those in Python.
- NEED to specity the types of values returned by the function and taken as parameters to the function , like int, void etc.
- type void has no possible values; the main function here returns nothing
- type String is like the str in Python
- the trailing ‘[]’ means array of . Arrays are like Python lists ,except that their size is fixed once created.
- Functions named main are special : they are what get called when one runs a Java program.(In python ,the main function is essentially anonymous)
- Any code we want to run must be inside of a function public static void main(String[] args)
- Function Headers in Java contain more information that those in Python.
-
Selection
- In python a.b means the thing names b that is in or that applies to the thing identified (or computed) by a
- Thus System.out means the variable named out that is found in the class named System
- Likewise System.out.println means the method named println that applies to the object referenced by the value of variable System.out
-
Access
- Every declared entity in Java has access permissions indicating what pieces of code may mention it.
- In particular , public classes , methods and variables may be referred to anywhere else in the program.
- We sometimes refer to them as exported from their class( for methods or variables) or package( for classes)
- Static method / variable
- Static methods and variables are “one-of” things
- A static method is just like an ordinary python function (outside of any class) or a function in a python class that annotated @staticmethod
- A static variable is like a Python variable defined outside of any class or a variable selected from a class ,as opposed to from a class instance.
- Other variables are local variables (in functions) or instance variables(in classes), and these are as in python.
-
*Using java the implement 'print(“hello world”)
public class Hello{
public static void main(String[] args){
System.out.println("hello world");
}
}
Basic code compared with Python
# python
class Car:
def __init__(self, m):
self.model = m
self.wheels = 4
def drive(self):
if self.wheels < 4:
print(self.model +" no go vroom")
return
print(self.model + " goes vroom") # driving makes noise
def getNumWheels(self):
return self.wheels
def driveIntoDitch(self, wheelsLost):
self.wheels = self.wheels - wheelsLost
c1 = Car("Civil type R")
c2 = Car("Toyota Camry")
c1.drive()
c1.driveIntoDitch(2)
c1.drive()
print(c2.getNumWheels())
// java
public class Car{
public String model;
public int