Python is a widely used interpretative, advanced and general-purpose programming language, created by Gido Van Rosum. The first edition was released in 1991. It can be seen as an improved LISP (adding the advantages of some other programming languages, such as object-oriented). Python's philosophy of design emphasizes code readability and concise grammar (especially the use of space indentation to divide code blocks rather than braces or keywords). Python allows developers to express ideas in less code than C++ or Java. Whether small or large programs, the language tries to make the structure of the program clear.
Java is a widely used computer programming language. It has the characteristics of cross-platform, object-oriented and generic programming. It is widely used in enterprise-level Web application development and mobile application development. The style of Java programming language is very close to that of C++ language. It inherits the core of object-oriented technology of C++ language, abandons the pointer which easily causes errors, replaces it with reference, removes operator overload and multiple inheritance features in C++ language, replaces it with interface, and adds garbage collector function. Generic programming, type-safe enumeration, variable-length parameters and automatic assembly/disassembly features are introduced in Java SE version 1.5. Sun Microsystems interprets Java as "a simple, object-oriented, distributed, explanatory, robust, system-independent, portable, high-performance, multi-threaded and dynamic language".
So what's the difference in grammar between Python and Java? Let's explore it with a few vivid examples.
Goal 1: Output hello, world
Python version
print("hello,world")
Java version
public class HelloWorld { //There HelloWorld Need to be the same as the filename public static void main(String[] args) { System.out.println("hello,world"); } }
Goal 2: Get keyboard input and output
Python version
name = input("Please enter your name:") print(name)
Java version
import java.util.Scanner; public class inputstr { public static void main(String[] args) { System.out.println("Please enter your name:"); String name = new Scanner(System.in).next(); System.out.println(name); } }
Goal 3: Output a random number from 1 to 10
Python version
import random
print(random.randint(1,10))
Java version
import java.util.Random; public class prandom { public static void main(String[] args) { System.out.println(new Random().nextInt(10)+1)
}
}
Goal 4: Specify a list or array, [51, 22, 93, 17, 77, 31, 44, 55, 20], please sort it quickly.
Python version
def quicksort(lis): if len(lis) < 2: return lis mid = lis[len(lis)//2] lis.remove(mid) left, right = [], [] for li in lis: if li >= mid: right.append(li) else: left.append(li) return quicksort(left) + [mid] + quicksort(right) if __name__ == "__main__": li = [51, 22, 93, 17, 77, 31, 44, 55, 20] newl = quicksort(li) print(newl)
Java version
import java.util.ArrayList; public class quickpractise { public static void main(String[] args) { int list[] = {51, 22, 93, 17, 77, 31, 44, 55, 20}; ArrayList<Integer> lis = new ArrayList<>(); for (int i=0;i<list.length;i++){ lis.add(list[i]); } ArrayList<Integer> newl = quicksort(lis); System.out.println(newl); } public static ArrayList<Integer> quicksort(ArrayList<Integer> lis) { if (lis.size() < 2) { return lis; } int len = lis.size()/2; int mid = lis.get(len); lis.remove(len); ArrayList<Integer> left = new ArrayList<>(); ArrayList<Integer> right = new ArrayList<>(); for (int i=0;i<lis.size();i++){ int a = lis.get(i); if (a>=mid){ right.add(a); } else{ left.add(a); } } ArrayList<Integer> left1 = quicksort(left); ArrayList<Integer> right1 = quicksort(right); left1.add(mid); for (int i=0;i<right1.size();i++){ left1.add(right1.get(i)); } return left1; } }
Finally, to sum up, every variable used in Java needs to define a type, and when a function is defined, it also needs to write the return type if there is a return value. The definition list in Python is very concise and does not need to be defined by specifying the number and content. The number defined in Java can not be changed after that. If it can be changed, it needs to be defined by ArrayList<> and the type also needs to be specified. The overall feeling is that in the same way, to complete a code, Python really wins perfectly, just in line with the sentence: "Life is short,I use python".
Small partners are welcome to pay attention and share their learning experience and materials from time to time.