scjp

SCJP6 regex issue

≯℡__Kan透↙ 提交于 2019-11-26 10:49:29
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 \d* matches 0 or more digits. So, it will even match empty string before every character and after the last character.

Java SneakyThrow of exceptions, type erasure

删除回忆录丶 提交于 2019-11-26 09:44:53
问题 Can someone explain this code? public class SneakyThrow { public static void sneakyThrow(Throwable ex) { SneakyThrow.<RuntimeException>sneakyThrowInner(ex); } private static <T extends Throwable> T sneakyThrowInner(Throwable ex) throws T { throw (T) ex; } public static void main(String[] args) { SneakyThrow.sneakyThrow(new Exception()); } } It may seems strange, but this doesn\'t produce a cast exception, and permits to throw a checked exception without having to declare it in the signature,

Java unreachable catch block compiler error

情到浓时终转凉″ 提交于 2019-11-26 06:45:08
问题 Why in Java can we catch an Exception even if it is not thrown, but we can\'t catch it\'s subclass (except for \"unchecked\" RuntimeException s and it subclasses). Example code: class Test { public static void main(String[] args) { try { // do nothing } catch (Exception e) { // OK } try { // do nothing } catch (IOException e) { // COMPILER ERROR: Unreachable catch block for IOException. //This exception is never thrown from the try statement body } } } Any ideas? 回答1: A RuntimeException could

How many String objects will be created

你说的曾经没有我的故事 提交于 2019-11-26 04:54:22
问题 I have the following Java code: public String makinStrings() { String s = \"Fred\"; s = s + \"47\"; s = s.substring(2, 5); s = s.toUpperCase(); return s.toString(); } The question is somehow simple: how many String objects will be created when this method is invoked? At the beginning I answered that 5 String objects are created, but the answer from my book says that only 3 objects are created and no explanation was given (this is a SCJP question). From my point of view there are 5 objects: \

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

谁说胖子不能爱 提交于 2019-11-26 02:37:39
问题 public class Java{ public static void main(String[] args){ final byte x = 1; final byte y = 2; byte z = x + y;//ok System.out.println(z); byte a = 1; byte b = 2; byte c = a + b; //Compiler error System.out.println(c); } } If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte. Why does it happen when we add two final bytes that fit in a byte? There is no compiler error. 回答1: From the JLS 5.2 Assignment Conversion In