一.控制台打印hello world
1.java输出hello world
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
在java里面我们采用System.out.println("Hello world!");的方式来输出hello world
2.golang输出hello world
package main
import "fmt"
func main() {
// 换行输出
fmt.Println("hello world")
// 一行输出
fmt.Printf("hello world")
// 输出
fmt.Print("hello world")
}
golang中控制台打印采用fmt包下的print方法,printf,println方法来打印
1)Print 和 Println 区别:
2)Println 和 Printf 区别:
Printf 是格式化输出,在很多场景下比 Println 更方便,举个例子:
二.定义变量和常量
1.java中定义变量和常量
java中的常量通过final关键字来定义,final
关键字用于表示一个值在初始化后不能再被修改。常量的命名通常使用全大写字母,多个单词之间使用下划线分隔。
package org.example;
public class Main {
public static void main(String[] args) {
// 定义常量MAX_AGE
final int MAX_AGE = 20;
// 定义变量maxAge
int maxAge = 20;
System.out.println(MAX_AGE);
System.out.println(maxAge);
}
}
java中的基本类型有byte,short,int,long,float,double,boolean,char
整数类型(Integral Types):
- byte:1 字节,范围为 -128 到 127。
- short:2 字节,范围为 -32,768 到 32,767。
- int:4 字节,范围为 -2,147,483,648 到 2,147,483,647。
- long:8 字节,范围为 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807。
浮点数类型(Floating-Point Types):
- float:4 字节,范围为约 ±3.40282347E+38F(有效位数为 6-7 位)。
- double:8 字节,范围为约 ±1.79769313486231570E+308(有效位数为 15 位)。
字符类型(Character Type):
- char:2 字节,表示一个 Unicode 字符,范围为 0 到 65,535。
布尔类型(Boolean Type):
- boolean:表示逻辑值,只有两个取值:
true
和false
。
2.Go语言中定义常量和变量
package main
import "fmt"
func main() {
// 定义常量
const MAX_AGE int = 20
// 定义变量
var maxAge int = 20
// 定义变量
maxAge2 := 20
fmt.Println(MAX_AGE)
fmt.Println(maxAge)
fmt.Println(maxAge2)
}
Go语言中使用const + 字段名 + 类型(可以不写) = 值,来定义常量
var + 字段名 + 类型(可以不写) = 值,来定义变量
也可以使用 字段名 + := 值,来定义变量(短变量只能用于声明局部变量,不能用于全局变量的声明)
Go语言基本类型有
整数类型(Integer Types):
- int8:1 字节,范围为 -128 到 127。
- int16:2 字节,范围为 -32,768 到 32,767。
- int32(也称为 rune):4 字节,范围为 -2,147,483,648 到 2,147,483,647。
- int64:8 字节,范围为 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807。
- uint8(也称为 byte):1 字节,范围为 0 到 255。
- uint16:2 字节,范围为 0 到 65,535。
- uint32:4 字节,范围为 0 到 4,294,967,295。
- uint64:8 字节,范围为 0 到 18,446,744,073,709,551,615。
- int:根据操作系统平台,为 32 位或 64 位。
- uint:根据操作系统平台,为 32 位或 64 位。
浮点数类型(Floating-Point Types):
- float32:4 字节,范围为约 ±1.18E-38 到 ±3.4E+38(有效位数为 7 位)。
- float64(也称为 float):8 字节,范围为约 ±2.23E-308 到 ±1.8E+308(有效位数为 15 位)。
复数类型(Complex Types):
- complex64:由两个 float32 组成的复数。
- complex128:由两个 float64 组成的复数。
字符串类型(String Type):
- string:表示一个字符串,由一系列的字节组成。
布尔类型(Boolean Type):
- bool:表示逻辑值,只有两个取值:
true
和false
。指针类型(Pointer Types):
- 指向其他类型的指针,例如
*int
表示指向int
类型的指针。
3.java于go的区别
1)定义多个变量
在java当中我们定义多个变量可以采用下面的方式:
package org.example;
public class Main {
public static void main(String[] args) {
int a = 1, b = 2;
String name = "xiaoDuanYu", sex = "男";
}
}
在java中一次定义多个变量只能定义相同的类型的变量
但是在Go语言中,我们可以一次性定义多个不同类型的变量例如:
package main
import "fmt"
func main() {
var (
name string = "xiaoDuanYu"
sex string = "男"
a int = 1
b int = 2
)
fmt.Println(name, sex, a, b)
}
2)计数器与匿名变量
在Go语言中有一个名为itoa的计数器,可以实现自增的功能(itoa只能用于常量里面)
package main
import "fmt"
func main() {
const (
n1 = iota //0
n2 //1
_
n4 //3
)
fmt.Println(n1, n2, n4)
}
在上面的代码中我们出现了一个下划线,在Go语言中_表示匿名变量,用于忽略某个值或占位,而不使用它
在java当中是不存在iota这种计数器的(java当中的计数一般采用count++),也不存在匿名变量
3)代码风格
在java当中我们每写完一行代码必须写“ ; ”来结束语句
在Go语言中不需要写“ ;”
4)变量是否需要使用
在java中可以定义了变量不使用
但是在Go语言中定义了的变量必须使用,否则将会报错