smali

понедельник, 26 сентября 2016 г.

Java – Read a file from resources folder


https://www.mkyong.com/java/java-read-a-file-from-resources-folder/

 this tutorial, we will show you how to read a file from a resources folder, in both Java and Unit Test environment. In simple, put files in a resources folder, and read the file with following code snippets :
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());

1. Project Directory

Review a Maven project Structure.




2. Classic Example
Example to read a file “test.txt” from a resources folder.
main/resources/file/test.txt
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
Hello.java
package com.mkyong;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Hello {

  public static void main(String[] args) {
 Hello obj = new Hello();
 System.out.println(obj.getFile("file/test.txt"));
  }

  private String getFile(String fileName) {

 StringBuilder result = new StringBuilder("");

 //Get file from resources folder
 ClassLoader classLoader = getClass().getClassLoader();
 File file = new File(classLoader.getResource(fileName).getFile());

 try (Scanner scanner = new Scanner(file)) {

  while (scanner.hasNextLine()) {
   String line = scanner.nextLine();
   result.append(line).append("\n");
  }

  scanner.close();

 } catch (IOException e) {
  e.printStackTrace();
 }

 return result.toString();

  }

}
Output
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

суббота, 24 сентября 2016 г.

9 Things about Null in Java


http://javarevisited.blogspot.sg/2014/12/9-things-about-null-in-java.html

Java and null are uniquely bonded. There is hardly a Java programmer, who is not troubled by null pointer exception, it is the most infamous fact about. Even inventor of null concept has called it his billion dollar mistake, then why Java kept it? Null was there from long time and I believe Java designer knows that null creates more problem than it solves, but still they went with it. It surprise me even more because Java's design philosophy was to simplify things, that's why they didn't bothered with pointers, operator overloading and multiple inheritance of implementation, they why null. Well I really don't know the answer of that question, what I know is that, doesn't matter how much null is criticized by Java developers and open source community, we have to live with that. Instead of ruing about null it's better to learn more about it and make sure we use it correct. Why you should learn about null in Java? because If you don't pay attention to null, Java will make sure that you will suffer from dreaded java.lang.NullPointerException and you will learn your lesson hard way. Robust programming is an art and your team, customer and user will appreciate that. In my experience, one of the main reasons of NullPointerException are not enough knowledge about null in Java. Many of you already familiar with null but for those, who are not, can learn some old and new things about null keyword. Let's revisit or learn some important things about null in Java.



What is Null in Java

As I said, null is very very important concept in Java. It was originally invented to denote absence of something e.g. absence of user, a resource or anything, but over the year it has troubled Java programmer a lot with nasty null pointer exception. In this tutorial, we will learn basic facts about null keyword in Java and explore some techniques to minimize null checks and how to avoid nasty null pointer exceptions.


1) First thing, first,  null is a keyword in Java, much like publicstatic or final. It's case sensitive, you cannot write null as Null or NULL, compiler will not recognize them and give error.

Object obj = NULL; // Not Ok
Object obj1 = null  //Ok

Are there any Java method ordering conventions?


  1. Class (static) variables: First the public class variables, then the protected, and then the private.
  2. Instance variables: First public, then protected, and then private.
  3. Constructors
  4. Methods: These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in between two public instance methods. The goal is to make reading and understanding the code easier.

How to Find the Median Value



It's the middle of a sorted list of numbers.

Median Value

The Median is the "middle" of a sorted list of numbers.

How to Find the Median Value

To find the Median, place the numbers in value order and find the middle.

Example: find the Median of 12, 3 and 5

Put them in order:
3, 5, 12
The middle is 5, so the median is 5.

 

One’s Complement and Two’s Complement


https://delightlylinux.wordpress.com/2014/10/13/binary-lesson-12-ones-complement-and-twos-complement/

One’s complement and two’s complement are two important binary concepts. Two’s complement is especially important because it allows us to represent signed numbers in binary, and one’s complement is the interim step to finding the two’s complement.

Two’s complement also provides an easier way to subtract numbers using addition instead of using the longer, more involving subtraction method discussed in Lesson 10.
We will save the two’s complement subtraction for another lesson, but here, we will look at what these terms mean and how to calculate them.

One’s Complement

If all bits in a byte are inverted by changing each 1 to 0 and each 0 to 1, we have formed the one’s complement of the number.
Original       One's Complement
-------------------------------
10011001  -->  01100110
10000001  -->  01111110
11110000  -->  00001111
11111111  -->  00000000
00000000  -->  11111111

Converting to one's complement.
Converting to one’s complement.
And that is all there is to it! One’s complement is useful for forming the two’s complement of a number.

binary search algorithm visualization

Arrays.binarySearch



https://www.dotnetperls.com/binarysearch-java

Arrays.binarySearch. A binary search algorithm uses guessing to quickly locate a value in a sorted array. It repeatedly chooses two elements. The next guess is based on their values.
In large arrays, binary search is much faster than a linear search. It is typically slower than a lookup table or hash table. But it may use less memory.
BinarySearch example. It is simple, but this example demonstrates binarySearch. Please notice the input array (values): it is presorted. We search for 8 in the array.
And:Value 8 is located at index 7. BinarySearch correctly located this value in the array.

Based on: Java 8

Java program that uses Arrays.binarySearch

import java.util.Arrays;

public class Program {
    public static void main(String[] args) {

 // A presorted array.
 int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 // Find value 8.
 int index = Arrays.binarySearch(values, 8);

 // Display result.
 System.out.println("Index = " + index);
 System.out.println("Value = " + values[index]);
    }
}

What is more efficient: System.arraycopy vs Arrays.copyOf?


http://stackoverflow.com/questions/2589741/what-is-more-efficient-system-arraycopy-vs-arrays-copyof

The difference is that Arrays.copyOf does not only copy elements, it also creates a new array. System.arrayCopy copies into an existing array.
Here is the source for Arrays.copyOf, as you can see it uses System.arraycopy internally to fill up the new array.
  public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}

http://hg.openjdk.java.net/jdk8/jdk8/jdk/


пятница, 23 сентября 2016 г.