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
193 views
in Technique[技术] by (71.8m points)

java - why not show exact value when Int value start from zero/0?

class Method{
    
        String name ;
        int Num ;
        
        void text() {
            System.out.println(" Hello this code is running coding by "+ name+" number " +Num);
        }
        
    
}
public class MethodClass {

    public static void main(String[] args) {
        
        Method Object = new Method();
        
        Object.name = " NAHIAN " ;
        
        Object.Num = 0123 ;
        
        Object.text();
        
    }

}

Result show:

 Hello this code is running coding by  NAHIAN  number 83

But the 83 should be 0123

question from:https://stackoverflow.com/questions/65662029/why-not-show-exact-value-when-int-value-start-from-zero-0

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

1 Reply

0 votes
by (71.8m points)

when you add a 0 in front, it is refering to an octa (base8) value. So 0123 is 83 in dec (base10)

To print out a number in octa. You can convert it to string using the following

String convertedString = Integer.toOctalString(0123);

System.out.println("The octa value is " + convertedString);

you can refer to https://www.javatpoint.com/java-decimal-to-octal for more information.


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

...