程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learn java basics from a python Perspective

編輯:Python

List of articles

  • 1. Variable
    • assignment
    • data type
  • 2. Symbol
    • Calculation operator
    • Comparison operator
    • Code symbol
    • notes
    • Text character
  • 3. if
    • a line if
    • A judgment
    • Multiple judgment
  • 4. for
  • 5. while
  • 6. Array
  • 7. Program structure
  • 8. Input and output
    • Output
    • Input
  • 9. Exception trapping

1. Variable

assignment

project JavaPythonJavaScriptVBA Must declare first yes no no no Statement int x; nothing nothing dim x% assignment x=1;x=1x=1x=1 Declare and assign values int x=1;x=1x=1 nothing empty nullNonenullundefinedNull

data type

project JavaPythonJavaScriptVBA Integers int x=1;x=1x=1x=1 character char a='A'; nothing nothing nothing character string String a="A";a="A"
a='A'a="A"
a='A'a="A" decimal float f=3.14f;
double d=1.7df=3.14f=3.14f=3.14 Boolean boolean b=true;b=Trueb=trueb=True Constant final double PI=3.14;PI=3.14const PI=3.14Const PI=3.14 object StringBuilder sb = new StringBuilder();
var sb = new StringBuilder();sb = ShaBi()sb = new Shabi()x = CreateObject("Scripting.Dictionary") Type conversion Only upward conversions are allowed allow allow allow

2. Symbol

Calculation operator

Operator JavaPythonJavaScriptVBA Add ++++ reduce ---- ride **** except //// Seeking remainder %%%mod The next power nothing 3**23**2 nothing Self increasing ++++ nothing nothing Self reduction ---- nothing nothing superposition +=+=+= nothing Reduplication -=-=-= nothing Multiplicative multiplication *=*=*= nothing Reduplication /=/=/= nothing Brackets ()()()() String connection ++++

Comparison operator

Operator JavaPythonJavaScriptVBA Greater than >>>> Greater than or equal to >=>=>=>= Less than <<<< Less than or equal to <=<=<=<= be equal to ======== It's not equal to !=!=!=!=and&&and&&andor||or||ornot!not!not

Code symbol

Symbol JavaPythonJavaScriptVBA Escape character \\\“” A newline ;:;: Whether the newline character can be omitted Do not omit Most of them can be omitted Most of them can be omitted can

notes

Symbol JavaPythonJavaScriptVBA Single-line comments //#//’ Multiline comment /*…*/“”"…"""
’’’…’’’/*…*/ nothing

Text character

Symbol JavaPythonJavaScriptVBA Single line character ’"
’"
’" Single line string ""
’"
’" Multiline string “”"…"""“”"…"""
’’’…’’’ nothing nothing

3. if

a line if

// Java
x = a > b ? c : d;
# Python
x = c if a > b else d
// JavaScript
x = a > b ? c : d
' VBA
if a > b Then x = c Else x = d

A judgment

// Java
if (a > b) {

x = c;
} else {

x = d;
}
# Python
if a > b:
x = c
else:
x = d
// JavaScript
if (a > b) {

x = c
} else {

x = d
}
' VBA
If a > b Then
x = c
Else
x = d
End If

Multiple judgment

// Java
if (a > b) {

x = c;
} else if (a > bb) {

x = cc;
} else {

x = d;
}
# Python
if a > b:
x = c
elif a > bb:
x = cc
else:
x = d
// JavaScript
if (a > b) {

x = c
} else if (a > bb) {

x = cc
} else {

x = d
}
' VBA
If a > b Then
x = c
ElseIf a > bb Then
x = cc
Else
x = d
End If

4. for

  • Subscript loop
// Java
for (int i=0;i<100;i++) {

System.out.println(i);
}
# Python
for i in range(100):
print(i)
// JavaScript
for (var i=0;i<100;i++) {

console.log(i)
}
' VBA
For i = 1 to 100 step 1
Debug.Print i
next
  • Array traversal loop
// Java
for (int a:arr) {

System.out.print(a);
}
# Python
for a in arr:
print(a)
// JavaScript
for (a in arr) {

console.log(a)
}
' VBA 
For Each a in arr
Debug.Print a
Next
project JavaPythonJavaScriptVBA Break the loop breakbreakbreakExit For Skip the loop continuecontinuecontinuegoto

5. while

// Java
int i;
while (i < 100) {

System.out.println(i);
i++;
}
// java Another while
int i;
do {

System.out.println(i);
i++;
} while (i < 99);
# Python
i = 0
while True:
if i < 100:
print(i)
else:
break
// JavaScript
i = 0
while (i < 100) {

console.log(i)
i++
}
' VBA
' 1
i = 0;
While i < 100
Debug.Print(i)
Wend
' VBA
' 2
i = 0;
Do While i < 100
Debug.Print(i)
Loop
' VBA
' 3
i = 0;
Do
Debug.Print(i)
Loop While i < 99
' VBA
' 4
i = 0;
Do Until i >= 100
Debug.Print(i)
Loop
' VBA
' 5
i = 0;
Do
Debug.Print(i)
Loop Until i >= 99
project JavaPythonJavaScriptVBA Break the loop breakbreakbreakExit Do Skip the loop continuecontinuecontinuegoto

6. Array

project JavaPythonJavaScriptVBA Definition int[] x = {1,2,3,4,5};x = [1,2,3,4,5]x = [1,2,3,4,5]dim Arr() Symbol {}[]
{}
()[]Array() Indexes x[0];x[0]x[0]Arr(0) Type mix Don't allow x=[1,'a']x=[1,'a']Arr=Array(1,"a") increase Don't allow x.append('b')
x.insert(0,'c')x.push('b')Redim Preserve Arr(4)
Arr(4) = 3 Delete Don't allow x.pop(1)
del x[1]x.pop(1)Redim Arr(1) Change x[0] = 6;x[0] = 6x[0] = 6Arr(0)=6

7. Program structure

  • Java
/** * Documentation Comments */
public class Hello {

public static void main(String[] args) {

// Main program description 
userFunction usf = new userFunction();
usf.setArg("Hello");
System.out.println(usf.getArg());
/* Multiline comment branch */
}
}
class userFunction {

private String arg;
public void setArg(String arg) {

// Set up 
this.arg = arg;
}
public String getArg() {

// return 
return this.arg;
}
}
  • Python
''' documentation '''
class userFunction:
def __init__(self):
pass
def setArg(self,arg):
self.arg = arg
def getArg(self):
return self.arg
if __name__ == '__main__':
usf = userFunction()
usf.setArg("Hello")
print(usf.getArg())
  • JavaScript
function userFunction(args) {

x = process(args)
return x
}
  • VBA
Sub userSub()
x = userFunction(args)
Debug.Print x
End Sub
Function userFunction(args) as String
userFunction = process(args)
End Function

8. Input and output

Output

project JavaPythonJavaScriptVBA Output System.out.println
System.out.printprintconsole.logDebug.Print Format output System.out.printf
System.out.formatformat nothing nothing Quick format nothing f'{d} is a number'`${d} is a number` nothing

Input

project JavaPythonJavaScriptVBA Input import java.util.Scanner

Scanner scanner = new Scanner(System.int);
String ipt = scanner.nextLine();ipt = input(' Please enter :')var ipt = prompt(' Please enter ',' Default ')ipt = InputBox(" Please enter ",," Default ")

9. Exception trapping

project JavaPythonJavaScriptVBA Exception trapping try {..}
catch {...}
finally {...}try:
except:
finally:try {..}
catch {...}
finally {...}On error goto tag

I think it will be more efficient to learn new code according to the previous code
Beginners Java There are also many people who don't understand , If there is a mistake , Please correct


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved