java变量名首字母小写,变量名称的首字母从大写变为小写

I created an iOS app that stores the data to Firebase realtime database like this:

users

- ID

-- Email: "test@domain.com"

-- UsersName: "test"

-- Level: 1

-- XP: 0

Now I started with Android development using Kotlin and the data is stored with the first letter of the values name lowercased like this:

users

- ID

-- email: "test@domain.com"

-- usersName: "test" (No mistake, the N is uppercased)

-- level: 1

-- xp: 0 (No mistake, the P is lowercase)

This is my code:

data class User(var UsersName: String = "",

var XP: Int = 0,

var Level: Int = 1,

var Email: String = "")

private fun saveUserToFirebaseDatabase(){

val uid = FirebaseAuth.getInstance().uid ?: ""

val ref = FirebaseDatabase.getInstance().getReference("users/$uid")

val mDatabase = FirebaseDatabase.getInstance().getReference();

val username = usernameSignUp.text.toString()

val user = User(username, 0, 1, SignUpEmailtext.text.toString())

print(user)

mDatabase.child("users").child(uid).setValue(user)

.addOnSuccessListener {

}

.addOnFailureListener{

}

}

I expect the names of the values to be exactly how they are named in the class (like the iOS app saves them).

解决方案

Kotlin works on JVM (Java Virtual Machine), so in order to be able to run a .kt file, first of all it should be compiled into a Java file. So when using a class that looks like:

data class User(var UsersName: String = "",

var XP: Int = 0,

var Level: Int = 1,

var Email: String = "")

The corresponding Java file is created and according to the Java Naming Conventions regading variables:

It should be a lowercase letter such as java, lang.

This means that doesn't matter if you choose the name of your fields to start either with an uppercase or a lowercase, all fiels are converted to start with a lowercase. That's why the properties in your database are present like that. Not only in Java/Kotlin is this convention available but also in other programming languages, so I recommend you to use this recommendation in your projects.

Regarding Kotlin, remember that at the end, everything is converted to byte code so it can work on JVM.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值