Where to use a StringBuffer/StringBuilder than a String in Java?

raja
Updated on 30-Jul-2019 22:30:26

184 Views

The String class objects are immutable whereas the StringBuffer and the StringBuilder objects are mutable.A StringBuffer is synchronized while a StringBuilder is not synchronized.A Concatenation operator "+" is internally implemented using either StringBuffer or StringBuilder.If the Object value is not going to change use String Class because a String object is immutable.If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.If the Object value can change and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.

How to disable close button on a JFrame in Java?

George John
Updated on 30-Jul-2019 22:30:26

677 Views

To disable the close button, let us first create a frame −JFrame frame = new JFrame("Login!");Use the setDefaultCloseOperation() to set the status of the close button. Set it to DO_NOTHING_ON_CLOSE to disable it −frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);The following is an example to disable close button on a JFrame in Java −Examplepackage my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Login!");       JLabel label1, label2, label3;       frame.setLayout(new GridLayout(2, 2));       label1 = ... Read More

Concatenate fields in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

260 Views

To concatenate fields, use the $concat operator. Let us first create a collection with documents −>db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Adam", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ebf46d78f205348bc62e") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ebfc6d78f205348bc62f") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ec376d78f205348bc630") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Sam", "StudentLastName":"Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ec436d78f205348bc631") }Following is the query to display all documents from a collection with the help of find() method −> db.concatenateFieldsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd6ebf46d78f205348bc62e"),    "StudentFirstName" : "Adam",    "StudentLastName" : "Smith" } ... Read More

HTML canvas strokeRect() Method

George John
Updated on 30-Jul-2019 22:30:26

299 Views

The strokeRect() method of the HTML canvas is used to create a rectangle on a web page. The element allows you to draw graphics on a web page using JavaScript. Every canvas has two elements that describes the height and width of the canvas i.e. height and width respectively.Following is the syntax −context.strokeRect(p, q, width, height);Above, p − The x-coordinate of the upper-left corner of the rectangleq − The y-coordinate of the upper-left corner of the rectanglewidth − Width of the rectangleheight − Height of the rectangleLet us now see an example to implement the strokeStyle property of canvas ... Read More

What are copy constructors in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

4K+ Views

Generally, the copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.Java supports for copy constructors but unlike C language, Java does not provide an explicit copy constructor you need to define it yourself.writing a copy constructorUsually, to initialize the values of instance variables of a class (one way) we create a parameterized constructor accepting the values for all instance variables and initialize them with the given values.int name; int age; public Student(String name, int age){ this.name = name; this.age ... Read More

HTML DOM Superscript Object

AmitDiwan
Updated on 30-Jul-2019 22:30:26

70 Views

The HTML DOM superscript Object represent the element of an HTML document.Let us see how to create superscript object −SyntaxFollowing is the syntax −document.createElement(“SUP”);ExampleLet us see an example of superscript object − Live Demo    body {       text-align: center;       background-color: #fff;       color: #0197F6;    }    h1 {       color: #23CE6B;    }    .btn {       background-color: #fff;       border: 1.5px dashed #0197F6;       height: 2rem;       border-radius: 2px;       width: 60%;       ... Read More

HTML DOM Local Storage clear() method

AmitDiwan
Updated on 30-Jul-2019 22:30:26

186 Views

The HTML DOM Local Storage clear() method is used for clearing the whole local storage entries.SyntaxFollowing is the syntax −localStorage.clear()OrsessionStorage.clear()ExampleLet us see an example for LocalStorage() method property − Live Demo LocalStorage clear()    form {       width:70%;       margin: 0 auto;       text-align: center;    }    * {       padding: 2px;       margin:5px;    }    input[type="button"] {       border-radius: 10px;    }    table, th, td {       border:1px solid black;       border-collapse: collapse;       margin: 0 ... Read More

What is the difference between size_t and int in C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

4K+ Views

Here we will see what are the differences between size_t and int in C++. If we consider the standard, both are integers of size 16 bits.On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably.One standard recommendation is that the size_t be at most as big as an unsigned long. So you may think that we can use unsigned long in the place of size_t, but unsigned long on 64-bit system, if the OS ins Windows, will be of 32-bits, but size_t will be of 64-bits.Read More

C++ Program to Implement Trie

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

Here we shall discuss a C++ Program to implement Trie. It is a tree based data structure, used for efficient retrieval of a key in a large data-set of strings.Functions and pseudocodesBegin function insert() :    If key not present, inserts key into trie. If the key is prefix of trie node, just mark leaf node. End Begin function deleteNode()    If tree is empty then return null.    If last character of the key is being processed,       then that node will be no more end of string after deleting it.       If given key ... Read More

Binary Countdown Protocol

Moumita
Updated on 30-Jul-2019 22:30:26

5K+ Views

Binary Countdown Protocol is a collision-free protocol that operates in the Medium Access Control (MAC) layer of the OSI model. In computer networks, when more than one station tries to transmit simultaneously via a shared channel, the transmitted data is garbled, an event called collision. Collision-free protocols resolves channel access while the stations are contending for the shared channel, thus eliminating any possibilities of collisions.Working Principle of Binary CountdownIn a binary countdown protocol, each station is assigned a binary address. The binary addresses are bit strings of equal lengths. When a station wants to transmit, it broadcasts its address to ... Read More

Advertisements