|
33 | 33 |  |
34 | 34 |
|
35 | 35 | ```bash |
36 | | - avinashbest $ javac Main.java |
| 36 | + avinashbest/ $ javac Main.java |
37 | 37 | ``` |
38 | 38 |
|
39 | 39 | ```bash |
40 | | - avinashbest $ cd .. |
| 40 | + avinashbest/ $ cd .. |
41 | 41 | ``` |
42 | 42 |
|
43 | 43 | ```bash |
44 | | - $ java com.avinashbest.Main |
| 44 | + src/ $ java com.avinashbest.Main |
45 | 45 | ``` |
46 | 46 |
|
47 | 47 | ## Interesting Facts about Java: |
|
285 | 285 | System.out.println("You are " + age); |
286 | 286 | System.out.println("Your name " + name); |
287 | 287 | } |
288 | | - } |
| 288 | + } |
289 | 289 | ``` |
290 | 290 |
|
291 | 291 | ## Mortgage Calculator: |
|
315 | 315 | System.out.println("Mortgage: " + NumberFormat.getCurrencyInstance().format(mortgage)); |
316 | 316 | } |
317 | 317 | } |
318 | | -``` |
| 318 | +``` |
| 319 | +
|
| 320 | +## Comparison Operators: |
| 321 | +
|
| 322 | +```bash |
| 323 | + public class Main { |
| 324 | + public static void main(String[] args) { |
| 325 | + int x = 1; |
| 326 | + int y = 1; |
| 327 | + System.out.println(x == y); |
| 328 | + System.out.println(x != y); |
| 329 | + System.out.println(x > y); |
| 330 | + System.out.println(x >= y); |
| 331 | + System.out.println(x < y); |
| 332 | + System.out.println(x <= y); |
| 333 | + } |
| 334 | + } |
| 335 | +``` |
| 336 | +
|
| 337 | +## Logical Operators: |
| 338 | +
|
| 339 | +```bash |
| 340 | + public class Main { |
| 341 | + public static void main(String[] args) { |
| 342 | + int temperature = 22; |
| 343 | + boolean isWarm = temperature > 20 && temperature < 30; |
| 344 | + System.out.println(isWarm); |
| 345 | + |
| 346 | + boolean hasHighIncome = true; |
| 347 | + boolean hasGoodCredit = true; |
| 348 | + boolean hasCriminalRecord = false; |
| 349 | + boolean isEligible = (hasGoodCredit || hasHighIncome) && !hasCriminalRecord; |
| 350 | + System.out.println(isEligible); |
| 351 | + } |
| 352 | + } |
| 353 | +``` |
| 354 | +
|
| 355 | +## if-statement: |
| 356 | +
|
| 357 | +```bash |
| 358 | + public class Main { |
| 359 | + public static void main(String[] args) { |
| 360 | + int temperature = 32; |
| 361 | + if (temperature > 32) { |
| 362 | + System.out.println("It's a hot day"); |
| 363 | + System.out.println("Drink water"); |
| 364 | + } else if (temperature > 20) { |
| 365 | + System.out.println("Beautiful day"); |
| 366 | + } else { |
| 367 | + System.out.println("Cold day"); |
| 368 | + } |
| 369 | + /*Simplifying if-statement*/ |
| 370 | + int income = 120_000; |
| 371 | + boolean hasHighIncome; |
| 372 | + if (income > 100_000) { |
| 373 | + hasHighIncome = true; |
| 374 | + } else { |
| 375 | + hasHighIncome = false; |
| 376 | + } |
| 377 | + /*Above Expression can be simplified by professional be like*/ |
| 378 | + int income = 120_000; |
| 379 | + boolean hasHighIncome = income > 100_000; |
| 380 | + } |
| 381 | + } |
| 382 | +``` |
| 383 | +
|
| 384 | +# Ternary Operators: |
| 385 | +
|
| 386 | +```bash |
| 387 | + public class Main { |
| 388 | + public static void main(String[] args) { |
| 389 | + int income = 120_000; |
| 390 | + String className; |
| 391 | + if (income > 100_000) { |
| 392 | + className = "First"; |
| 393 | + } else { |
| 394 | + className = "Economy"; |
| 395 | + } |
| 396 | + /*Simplification can be done as*/ |
| 397 | + income = 120_000; |
| 398 | + className = "Economy"; |
| 399 | + if (income > 100_000) { |
| 400 | + className = "First"; |
| 401 | + } |
| 402 | + /*More Simplification can be done using Ternary Operator*/ |
| 403 | + income = 120_000; |
| 404 | + className = (income > 100_000) ? "First" : "Economy"; |
| 405 | + } |
| 406 | + } |
| 407 | +``` |
0 commit comments