Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

java - Passing char into a method with an int parameter

The output from the following code is 123 because substring takes from beginIndex to EndIndex - 1. However, I am surprised how char here is understood as 3 (int) because substring take two ints. What is the concept behind this?

String x = "12345";
char a = 3;
x = x.substring(0, a);
System.out.println(x);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This goes all the way back to C, where char is in essence a narrow integer type and gets implicitly converted to int whenever necessary.

In Java, this is technically known as a "widening primitive conversion", and is covered in section 5.1.2 of the JLS.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...