Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 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


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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...