# 3 Operators and Assignments

 3.1 The following program will compile and run without errors:```// Filename: Sunlight.java public class Sunlight { public static void main(String[] args) { // Distance from sun (150 million kilometers) /* The max value for int is 2147483647, so using int here will work. */ int kmFromSun = 150000000; // Again, using int for this value is OK. int lightSpeed = 299792458; // Meters per second // Convert distance to meters. /* The result of this equation will not fit in an int. Let's use a long instead. We need to ensure that the values that are multiplied really are multiplied using long data types, not multiplied as int data types and later converted to long. The L suffix on the 1000L integer literal ensures this. The value of kmFromSun will implicitly be converted from int to long to match the data type of the other factor. The conversion can be done implicitly by the compiler since the conversion represents a widening of the data type. */ long mFromSun = kmFromSun * 1000L; /* We know that the result value will fit in an int, but the compiler does not. We use an explicit cast to convince the compiler. The conversion must be specified explicitly, since the conversion represents a narrowing of the data type. */ int seconds = (int) (mFromSun / lightSpeed); System.out.print("Light will use "); printTime(seconds); System.out.println(" to travel from the sun to the earth."); } /* We leave this method alone. */ public static void printTime(int sec) { int min = sec / 60; sec = sec - (min*60); System.out.print(min + " minute(s) and " + sec + " second(s)"); } } ``` 3.2 ```public class Binary { public static void main(String[] args) { System.out.println(makeBinaryString(42)); } public static String makeBinaryString(int i) { /* This section could have been optimized using StringBuffer, but is presented in this way for the sake of simplicity. */ String binary = ""; do { int lowBit = (i&1); String newDigit = ((lowBit == 0) ? "0" : "1"); binary = newDigit + binary; i >>>= 1; } while (i != 0); return binary; } } ``` 3.3 The following program will operate as specified in the exercise. When given no arguments, the program will not print anything.```public class ArgumentSkipper { public static void main(String[] args) { int count = args.length; // Iterate over the arguments skipping two places // forward between each step. for (int i=0; i < count; i+= 2) { System.out.println(args[i]); } } } ```  List of Figures  List of Tables  List of Examples  Foreword  Preface  Chapter 1. Basics of Java Programming  Chapter 2. Language Fundamentals  Chapter 3. Operators and Assignments  Chapter 4. Declarations and Access Control  Chapter 5. Control Flow, Exception Handling, and Assertions  Chapter 6. Object-oriented Programming  Chapter 7. Nested Classes And Interfaces  Chapter 8. Object Lifetime  Chapter 9. Threads  Chapter 10. Fundamental Classes  Chapter 11. Collections and Maps  Appendix A. Taking the SCPJ2 1.4 Exam  Appendix B. Objectives for the SCPJ2 1.4 Exam  Appendix C. Objectives for the Java 2 Platform Upgrade Exam  Appendix D. Annotated Answers to Review Questions  Appendix E. Solutions to Programming Exercises  1 Basics of Java Programming  2 Language Fundamentals  3 Operators and Assignments  4 Declarations and Access Control  5 Control Flow, Exception Handling, and Assertions  6 Object-oriented Programming  7 Nested Classes and Interfaces  8 Basics of Java Programming  9 Threads  10 Fundamental Classes  11 Collections and Maps  Appendix F. Mock Exam  Appendix G. Number Systems and Number Representation  Appendix H. About the CD  Single-User License Agreement