scjp

What are “connecting characters” in Java identifiers?

走远了吗. 提交于 2019-12-17 02:39:11
问题 I am reading for SCJP and I have a question regarding this line: Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! It states that a valid identifier name can start with a connecting character such as underscore. I thought underscores were the only valid option? What other connecting characters are there? 回答1: Here is a list of connecting characters. These are characters used to

Why does Double.NaN==Double.NaN return false?

匆匆过客 提交于 2019-12-17 02:28:30
问题 I was just studying OCPJP questions and I found this strange code: public static void main(String a[]) { System.out.println(Double.NaN==Double.NaN); System.out.println(Double.NaN!=Double.NaN); } When I ran the code, I got: false true How is the output false when we're comparing two things that look the same as each other? What does NaN mean? 回答1: NaN means "Not a Number". Java Language Specification (JLS) Third Edition says: An operation that overflows produces a signed infinity, an operation

SCJP6 regex issue

此生再无相见时 提交于 2019-12-17 02:00:09
问题 I have issue with following example: import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group()); } } } And the command line: java Regex2 "\d*" ab34ef Can someone explain me, why the result is: 01234456 regex pattern is d* - it means number one or more but there are more positions that in args[1], thanks 回答1: \d* matches 0

Why we must handle exception for method not throwing exceptions? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 06:21:41
问题 This question already has answers here : use of exception in inheritance (6 answers) Closed 6 years ago . Given public class ToBeTestHandleException{ static class A { void process() throws Exception { throw new Exception(); } } static class B extends A { void process() { System.out.println("B "); } } public static void main(String[] args) { A a = new B(); a.process(); } } Why we should handle the exception at line (a.process()) ?.The method process of class B does not throw exception at all?

SCJP with label

醉酒当歌 提交于 2019-12-13 05:03:00
问题 Below will be compilation fail due to "label z is missing" but if I just move z: to one step below after o = o + 2 then that will work? What is the logic behind this? public class Breaker { static String o = ""; public static void main(String[] args) { z: o = o + 2; for (int x = 3; x < 8; x++) { if (x == 4) break; if (x == 6) break z; o = o + x; } System.out.println(o); } } 回答1: You cannot put the labels everywhere in the code. it should be only before statements. in this case labelname: for(

Inconsistent behaviour of primitive integer types in Java

大城市里の小女人 提交于 2019-12-12 10:50:50
问题 Can someone explain to me like I'm five why I get different behaviour for two of four primitive types representing integers in Java? AFAIK all four are signed and they all use the most significant bit as a sign bit, so why do byte and short behave normally, and int and long act, well, strange? The fragment of oracle docs explaining this would be perfect. byte a = (byte) (Math.pow(2, 7)-1); //127 - as expected short b = (short) (Math.pow(2, 15)-1); //32767 - as expected int c = (int) (Math.pow

SCJP: Program not terminating after uncaught exception

白昼怎懂夜的黑 提交于 2019-12-11 12:19:43
问题 public class Threads2 implements Runnable { public void run() { System.out.println("run."); throw new RuntimeException("Problem"); } public static void main(String[] args) { Thread t = new Thread(new Threads2()); t.start(); System.out.println("End of method."); } } I predicted the output as run. //exception But it is showing output as, run exception end of method (or) run end of method exception I wonder, once the exception has occurred the program will terminate, right? 回答1: No, your program

method-local inner class cannot use variables declared within the method

醉酒当歌 提交于 2019-12-11 08:43:44
问题 Why a method-local inner class can't use variables declared inside the enclosing method except those marked final, i know that the variables declared inside the enclosing method might vanishes while the inner class instance remains valid, but what has changed when this variable/s is declared final? 回答1: The reason is that it is specified in the Java Language Specification #8.1.3 Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be

Prority based threads?

↘锁芯ラ 提交于 2019-12-11 04:26:20
问题 I'm studying thread priorities and I have both windows 10 and Ubuntu 16.0 lts operating system. And I got to know that windows doesn't provide priority based processing so that I can't see the use of priority based thread programs that how it is actually work . So I run my priority based program in ubuntu because someone told me that ubuntu provide priority based process. but when i run my program it show the same output or mixed output as windows. So is there any way to enable priority in

What is the effect of the dot (.) in the Java classpath?

你。 提交于 2019-12-10 15:55:42
问题 This is an example question from "SCJP mock exam": Given the default classpath: /foo And this directory structure: foo | test | xcom |--A.class |--B.java And these two files: package xcom; public class A { } package xcom; public class B extends A { } Which allows B.java to compile? (Choose all that apply.) A. Set the current directory to xcom then invoke javac B.java B. Set the current directory to xcom then invoke javac -classpath . B.java C. Set the current directory to test then invoke